Java 使用自定义标记集合在JSTL中切换语句/迭代
HelloTag.JavaJava 使用自定义标记集合在JSTL中切换语句/迭代,java,jsp,jakarta-ee,jstl,Java,Jsp,Jakarta Ee,Jstl,HelloTag.Java public class HelloTag extends SimpleTagSupport { @Override public void doTag() throws JspException, IOException { JspWriter out = getJspContext().getOut(); ArrayList outerList = new ArrayList(); ArrayList innerList = null;
public class HelloTag extends SimpleTagSupport {
@Override
public void doTag() throws JspException, IOException {
JspWriter out = getJspContext().getOut();
ArrayList outerList = new ArrayList();
ArrayList innerList = null;
for (int i = 0; i < 5; i++) {
innerList = new ArrayList();
innerList.add("1");
innerList.add("Name");
outerList.add(innerList);
}
for (int i = 0; i < outerList.size(); i++) {
for (int j = 0; j < innerList.size(); j++) {
out.println(innerList.get(j));
}
}
}
}
公共类HelloTag扩展了SimpleTagSupport{
@凌驾
public void doTag()抛出JSPEException,IOException{
JspWriter out=getJspContext().getOut();
ArrayList outerList=新的ArrayList();
ArrayList innerList=null;
对于(int i=0;i<5;i++){
innerList=新的ArrayList();
内部列表。添加(“1”);
内部列表。添加(“名称”);
添加(内部列表);
}
对于(int i=0;i
在JSP文件中,
有以下代码段:
<body>
<ct:Hello></ct:Hello>
</body>
当我运行JSP文件时,这个文件显示了准确的结果;但是
我想对来自自定义标记类的每个值作出决定
比如
<c:set var="name" scope="" value=""/>
<c:choose>
<c:when test="${name == 1}">
This is Your ID:-
</c:when>
<c:otherwise>
This is Your Name
</c:otherwise>
</c:choose>
这是您的身份证:-
这是你的名字
上面的代码只是为了示例。请告诉我如何决定来自自定义标记类的每个值
另一种解释我的问题的方法是,我想将每个值存储在一个变量中,然后仅使用JSTL而不使用Scriplet标记来决定该值,重点是上面的场景(HelloTag.Java),实际上不清楚您在问什么。但是你的标签,实际上,只是在外部列表的每个内部列表中循环(事实上,我想它应该这样做,但是它有一个bug,所以它没有) 您不需要自定义标记来完成这项工作,因为JSTL
标记已经完成了这项工作。假设您在请求(或页面、会话或应用程序)属性中存储了一个outerList:
<%-- iterate through the outer list --%>
<c:forEach var="innerList" items="${outerList}">
<%-- iterate through the innerList --%>
<c:forEach var="element" items="${innerList}">
<%-- do what you want with the element --%>
</c:forEach>
</c:forEach>
据我所知,SimpleTagSupport不允许您拥有JSP正文内容,如果您正在实现类似的内容,那么您需要使用BodyTagSupportDear@Dapeng,我将感谢您提供一个示例解决方案
<%-- iterate through the outer list --%>
<c:forEach var="person" items="${personList}">
ID : ${person.id}<br/>
Name : <c:out value="${person.name}"/>
</c:forEach>