2013-12-16 72 views
0

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

在上面的代码我能够访问<c:forEach>标签内的test值。
我需要创建一个具有类似功能的自定义标签。
我已经从oracle docs http://docs.oracle.com/javaee/5/tutorial/doc/bnamu.html获得了标题Declaring Tag Variables for Tag Handlers的信息。
任何人都可以帮助我实现相同的例子。

回答

0

嗨,我已经通过以下方式

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

创建变量

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} 
0

对于这么简单的东西的getter setter方法解决了这个问题,你可能会更好使用标记文件,这使得它易于使用正常jsp语法的一些小增加来创建标签库(并且是标准的一部分)

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

使用属性声明做

A“与变量标签”,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文件。