这是我在jsp文件中拥有的自定义标签:
<myTags:myTag name="John">
Value of k: ${valueOfK}
<br />
</myTags:myTag>
还有我的标记处理程序类:
@Override
public void doTag() throws JspException, IOException {
getJspContext().getOut().print("<table>");
for (int i = 0; i < 10; i++) {
getJspContext().getOut().print("<tr>");
for (int k = 0; k < i; k++) {
getJspContext().getOut().print("<td>" + name + "</td>");
getJspContext().setAttribute("valueOfK",k);
}
getJspBody().invoke(null);
getJspContext().getOut().print("</tr>");
}
getJspContext().getOut().print("</table>");
}
因此输出将是:
Value of k:
Value of k: 0
Value of k: 1
Value of k: 2
Value of k: 3
Value of k: 4
Value of k: 5
Value of k: 6
Value of k: 7
Value of k: 8
John
John John
John John John
John John John John
John John John John John
John John John John John John
John John John John John John John
John John John John John John John John
John John John John John John John John John
但是我想要实现的是这样的:
John Value of k: 1
John John Value of k: 2
等等...
为什么先打印所有k个值,然后构造表格?
最佳答案
为什么需要自定义标记,而使用内置JSTL标记却可以实现相同的目的。
样例代码:
<table>
<c:forEach begin="1" end="10" varStatus="status">
<tr>
<td>
<c:forEach begin="1" end="${status.index}">
John
</c:forEach>
Value of k: ${status.index}
</td>
</tr>
</c:forEach>
</table>
输出:
John Value of k: 1
John John Value of k: 2
John John John Value of k: 3
John John John John Value of k: 4
John John John John John Value of k: 5
John John John John John John Value of k: 6
John John John John John John John Value of k: 7
John John John John John John John John Value of k: 8
John John John John John John John John John Value of k: 9
John John John John John John John John John John Value of k: 10
如果在多个jsp中需要相同的内容,则将代码移动到单独的JSP文件中,并在需要时将其包括在内。
<jsp:include page="mytags.jsp">
<jsp:param value="Koray" name="name" />
</jsp:include>
mytags.jsp:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<table>
<c:forEach begin="1" end="10" varStatus="status">
<tr>
<td><c:forEach begin="1" end="${status.index}">
${param.name}
</c:forEach> Value of k: ${status.index}</td>
</tr>
</c:forEach>
</table>
如果要使用自定义标记,请尝试使用实现
BodyTagSupport
接口的BodyTag
。样例代码:
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class MyTag extends BodyTagSupport {
private String name;
private int counter;
public int doStartTag() throws JspException {
counter = 1;
JspWriter out = pageContext.getOut();
try {
out.print(name);
pageContext.setAttribute("valueOfK", counter);
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
return EVAL_BODY_INCLUDE;
}
public int doAfterBody() {
counter++;
if (counter == 10) {
return SKIP_BODY;
} else {
JspWriter out = pageContext.getOut();
try {
StringBuilder names = new StringBuilder();
for (int k = 0; k < counter; k++) {
names.append(name).append(" ");
}
out.print(names.toString());
pageContext.setAttribute("valueOfK", counter);
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
return EVAL_BODY_AGAIN;
}
}
public int doEndTag() throws JspException {
return EVAL_PAGE;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
关于java - 如何使此自定义标签正常工作?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25124816/