Tags 带有变量的自定义标记

Tags 带有变量的自定义标记,tags,taglib,custom-tags,Tags,Taglib,Custom Tags,我正在尝试对变量使用自定义标记。 对于eg) ${test} 在上面的代码中,我可以访问标记内的测试值。 我需要创建具有类似功能的自定义标记。 我从oracle文档http://docs.oracle.com/javaee/5/tutorial/doc/bnamu.html在标题下为标记处理程序声明标记变量 有人能帮我用例子来实现吗。嗨,我用以下方法解决了这个问题 class: test.java public void doTag() throws JspException

我正在尝试对变量使用自定义标记。
对于eg)

${test}

在上面的代码中,我可以访问
标记内的
测试值。
我需要创建具有类似功能的自定义标记。
我从oracle文档
http://docs.oracle.com/javaee/5/tutorial/doc/bnamu.html
在标题下
为标记处理程序声明标记变量


有人能帮我用例子来实现吗。

嗨,我用以下方法解决了这个问题

class: test.java    
    public void doTag() throws JspException, IOException {
    getJspContext().getOut().flush();
    //any method or operation
    super.doTag();
    getJspContext().setAttribute(variable, "Hello");
}
为变量创建getter setter

tld file:  
<tag>
    <name>test</name>
    <tag-class>com.org.test</tag-class>
    <body-content>empty</body-content>
    <attribute>
        <name>inputValue</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
        <name>variable</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
</tag>

jsp file:
       <%@ taglib uri="/WEB-INF/tld/tldfilename.tld" prefix="tag" %>
       <tag:test inputValue="hello" variable="testValue"/>
       ${testValue}
tld文件:
测试
com.org.test
空的
输入值
真的
真的
变量
真的
真的
jsp文件:
${testValue}

对于如此简单的事情,您最好使用标记文件,这使得在普通jsp语法中添加一些小的内容(并且是标准的一部分)就可以轻松创建标记库

“带变量的标记”是使用属性声明完成的,JSP代码非常简单:

<%@tag pageEncoding="utf-8" %>
<%-- dont forget to add declaration for JSTL here -->
<%@attribute name="test" type="java.lang.String" required="true" %>
<c:forEach var="test" items="itemstest">

    ${test}

</c:forEach>

${test}
请参阅whre上的链接文档,以放置和命名这些文件,使它们可以在您自己的jsp文件中访问