简体   繁体   English

JSP-自定义Taglib的问题

[英]JSP - issue with custom Taglib

I just tried to add a custom taglib to my project, such that the testtaglib.tld file contains: 我只是尝试将自定义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>

And then I added taglib class TestTaglib.java 然后我添加了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());
        }
    }
}

And then in .jsp file 然后在.jsp文件中

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

Okay the issue is: TestTaglib.java is recognizing values of testCode as ${testCode} and not the original value. 好的问题是: TestTaglib.java的值testCode${testCode}而不是原始值。 Any suggestion? 有什么建议吗?

Hi all inbuilt tag already handles the expression language. 大家好,所有内置标记已处理表达式语言。 Just change your code as mentioned below and it will work fine. 只需按以下所述更改您的代码即可,它将正常工作。

 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 is class provided under org.apache.taglibs.standard.tag.el.core package. ExpressionUtil是在org.apache.taglibs.standard.tag.el.core软件包下提供的类。

Here is short desc of evalNotNull method args 这是evalNotNull方法args的简短说明

1) tagName : your tag name is test 1)tagName:您的标签名称为test
2) tagAttribute: to eval in your case it is testCode 2)tagAttribute:在您的情况下为testCode
3) expression : which is el expression ${testCode} 3)表达式: el表达式$ {testCode}
4) Value: Class of expression value whether it is Boolean,String or any Object 4)值:表达式值的类别,无论是布尔值,字符串还是任何对象
5) tagClass: Reference of tag handler class so you can pass this 5)tagClass:标记处理程序类的引用,因此您可以传递它
6) pageContext: which is coming from TagSupport 6)pageContext:来自TagSupport

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM