1

これは、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 値が最初に出力され、次にテーブルが構築されるのはなぜですか?

4

2 に答える 2

1

出力の最も可能性の高い原因は、「k の値: 1」が td タグにないことです。出力に何が起こっているかというと、td タグ内にないテーブル タグ内のテキストは、あなたの場合のようにテーブルの先頭にプッシュされます。生成された html ソースを見ると、それが正しいことがわかります。

これで根本的な原因がわかったので、解決策を練ることができると思います...乾杯

これがあなたのために働くべきものです

k の値を印刷しないでください: 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().getOut().print("<td>Value of k: " + (k + 1) + "</td>");
            getJspContext().setAttribute("valueOfK",k);
        }
        getJspBody().invoke(null);
        getJspContext().getOut().print("</tr>");
    }
    getJspContext().getOut().print("</table>");
}
于 2014-08-04T19:19:52.390 に答える
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インターフェースを試してください。

ここに画像の説明を入力

サンプルコード:

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;
    }
}
于 2014-08-05T04:28:14.570 に答える