0

我正在尝试将自定义标签与变量一起使用。
例如)
<c:forEach var="test" items="itemstest">
${test}
</c:forEach>

在上面的代码中,我可以访问标签test内的值。 我需要创建一个具有类似功能的自定义标签。 我从标题下的 oracle 文档中获得了信息。 任何人都可以帮助我通过示例实现相同的功能。<c:forEach>

http://docs.oracle.com/javaee/5/tutorial/doc/bnamu.htmlDeclaring Tag Variables for Tag Handlers

4

2 回答 2

0

嗨,我已经通过以下方式解决了

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}
于 2014-01-03T08:43:13.920 回答
0

对于如此简单的事情,您最好使用标记文件,这样可以很容易地使用对正常 jsp 语法的一些小添加来创建标记库(并且是标准的一部分)

http://docs.oracle.com/javaee/1.4/tutorial/doc/JSPTags5.html

使用属性声明完成“带有变量的标记”,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>

请参阅有关 whre 放置和命名文件的链接文档,以使它们可以在您自己的 jsp 文件中访问。

于 2014-04-26T10:48:09.703 回答