如何使此自定义标记按预期工作?

时间:2014-08-04 18:04:22

标签: java jsp tags taglib custom-tags

所以这是我在jsp文件中的自定义标记:

<myTags:myTag name="John">
    Value of k: ${valueOfK}
    <br />
</myTags:myTag>

我的Tag Handler Class:

@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值然后构造表?

2 个答案:

答案 0 :(得分:1)

输出的最可能原因是&#34; k的值:1&#34;不在td标签中。输出中发生的事情是,表格标签内的任何不在td标签内的文本都会被推送到表格的开头,就像你的情况一样。查看生成的html源代码,您会发现它是真的。

现在你有根本原因所以我想你可以解决这个问题......干杯

以下是适用于您的内容

不要从你的jsp打印k的值:

<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().getOut().print("<td>Value of k: " + (k + 1) + "</td>");
            getJspContext().setAttribute("valueOfK",k);
        }
        getJspBody().invoke(null);
        getJspContext().getOut().print("</tr>");
    }
    getJspContext().getOut().print("</table>");
}

答案 1 :(得分:1)

为什么需要自定义标记,而使用内置JSTL标记可以实现同样的目标。

示例代码:

<table>
    <c:forEach begin="1" end="10" varStatus="status">
        <tr>
            <td>
                <c:forEach begin="1" end="${status.index}">
                    John&nbsp;
                </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}&nbsp;
                    </c:forEach> Value of k: ${status.index}</td>
        </tr>
    </c:forEach>
</table>

如果您想使用自定义标记,请尝试使用BodyTagSupport接口实现BodyTag

enter image description here

示例代码:

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;
    }
}
相关问题