【问题标题】:Custom JSP TLD involving both Attributes and elements涉及属性和元素的自定义 JSP TLD
【发布时间】:2016-03-22 03:59:14
【问题描述】:

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

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

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

我的顶级域名如下:

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

【问题讨论】:

    标签: java spring jsp tags


    【解决方案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);
        }
    }
    

    【讨论】:

    • 为什么要在 endTag 中而不是 doStartTag() 中?
    • 嗯,你不需要。您可以在 doStartTag 中执行此操作。我从我的一个标签源中复制了它,它需要读取标签的主体,所以这就是我在 endTag 中这样做的原因。但是是的,你可以在 doStartTag 中做到这一点。
    • 我明白了。感谢您的回复!
    • 欢迎您,感谢您接受我的回答 :)
    • 遗漏了一件事,为父母做doEndTag。我们需要清理 doEndTag 中的上下文。这样它就不会干扰另一个 ParamGroup 标签。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多