簡體   English   中英

JSP-自定義Taglib的問題

[英]JSP - issue with custom Taglib

我只是嘗試將自定義taglib添加到我的項目中,以便testtaglib.tld文件包含:

<taglib>
<tlibversion>1.0</tlibversion>
  <jspversion>1.1</jspversion>
  <shortname>name</shortname>

    <tag>
        <name>test</name>
        <tagclass>taglib.TestTaglib</tagclass>
        <bodycontent>empty</bodycontent>
        <attribute>
            <name>testCode</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
            <type>java.lang.String</type>
        </attribute>
    </tag>
<taglib>

然后我添加了taglib類TestTaglib.java

public class TestTaglib extends TagSupport {

    private String  testCode;
    public int doStartTag() throws JspException {
        try {
            JspWriter out = pageContext.getOut();
            //doing some conversion with testCode
            out.print(testCode);
            return EVAL_PAGE;
        } catch(IOException ioe) {
            throw new JspException("Error: " + ioe.getMessage());
        }
    }
}

然后在.jsp文件中

<name:test testCode="${testCode}"/>

好的問題是: TestTaglib.java的值testCode${testCode}而不是原始值。 有什么建議嗎?

大家好,所有內置標記已處理表達式語言。 只需按以下所述更改您的代碼即可,它將正常工作。

 public class TestTaglib extends TagSupport {


 private String  testCode;
    public int doStartTag() throws JspException {
        try {
            JspWriter out = pageContext.getOut();
            //doing some conversion with testCode
            String value = (String) ExpressionUtil.evalNotNull("test", "testCode", testCode, String.class, this, pageContext);
            out.print(value);
            return EVAL_PAGE;
        } catch(IOException ioe) {
            throw new JspException("Error: " + ioe.getMessage());
        }
    }
}

ExpressionUtil是在org.apache.taglibs.standard.tag.el.core軟件包下提供的類。

這是evalNotNull方法args的簡短說明

1)tagName:您的標簽名稱為test
2)tagAttribute:在您的情況下為testCode
3)表達式: el表達式$ {testCode}
4)值:表達式值的類別,無論是布爾值,字符串還是任何對象
5)tagClass:標記處理程序類的引用,因此您可以傳遞它
6)pageContext:來自TagSupport

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM