涉及属性和元素的自定义JSP TLD

时间:2016-03-22 03:59:15

标签: java spring jsp tags

我正在尝试为基于Spring 3的应用程序创建一个自定义TLD,其格式如下:

<prefix:mytag attribute1="myvalue">
    <element>abcd</element>
    <element>abcd</element>
    <action>cancel</action>
</prefix:mytag>

为此,我正在扩展 javax.servlet.jsp.tagext.BodyTagSupport 并定义属性 attribute1 但是如何定义元素?

我的TLD如下:

<tag>
    <description>My custom TLD</description>
    <name>mytag</name>
    <tag-class>mypackage.MyCustomTLD</tag-class>
    <attribute>
        <description>Hold Attribute</description>
        <name>attribute1</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
</tag>

1 个答案:

答案 0 :(得分:1)

您无法定义标记的元素,它们被视为单独的标记。

您还需要为每个子元素定义标记。这是一个例子。

<app:paramGroup id="edit">
     <app:param id="name" value="attribute"/>
     <app:param id="attId" name="attForm" property="id"/>
</app:paramGroup>

为了实现上述功能,您需要定义两个标签。

<tag>
    <name>paramGroup</name>
    <tagclass>com.mycompany.taglib.ParamGroupTag</tagclass>
    <info>
        Param Group Tag used to define the parameters group required for the struts html link
    </info>

    <attribute>
        <name>id</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
</tag>


<tag>
    <name>param</name>
    <tagclass>com.mycompany.taglib.ParamTag</tagclass>

    <bodycontent>empty</bodycontent>

    <attribute>
        <name>id</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>

    <attribute>
        <name>name</name>
        <required>false</required>
        <rtexprvalue>true</rtexprvalue>
    </attribute>
</tag>

这里是标记类,父标记实例应该发布到上下文,以便子标记可以与父标记交互。

public class ParamGroupTag extends BodyTagSupport {

    public static final String NAME = "com.mycompany.taglib.ParamGroupTag";

    public int doStartTag() throws JspException {

        boolean exist = false;

        params = new HashMap();

        // your initialization code

        // Store this tag itself as a page attribute
        pageContext.setAttribute(ParamGroupTag.NAME, this);

        // Continue processing this page
        return (EVAL_BODY_BUFFERED);
    }

    /**
     * Adds the paramter this param object
     * @param paramName Parameter Name
     * @param value     Parameter Value
     */
    public void addParam(String paramName, Object value) {
        params.put(paramName, value);
    }

    /**
     * Clean up the Tag.
     *
     * @exception JspException if a JSP exception has occurred
     */
    public int doEndTag() throws JspException {

        // tag functionality here

        // Remove the Tag from pageContext
        pageContext.removeAttribute(ParamGroupTag.NAME);

        // Continue processing this page
        return (EVAL_PAGE);
    }

}


public class ParamTag extends BodyTagSupport {

    /**
     * Reads the Params and Publishes to ParamGroup
     *
     * @exception JspException if a JSP exception has occurred
     */
    public int doStartTag() throws JspException {

        ParamGroupTag paramGroupTag = (ParamGroupTag)
                    pageContext.getAttribute(ParamGroupTag.NAME);

        if (paramGroupTag == null) {
            throw new JspException("ParamTag must be used with in ParamGroup Tag");
        }

        // read the attribtues

        paramGroupTag.addParam(id, value);

        // Continue processing this page
        return (EVAL_PAGE);
    }
}