繁体   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