【问题标题】:Extra attributes from Spring <form:form>Spring <form:form> 的额外属性
【发布时间】:2011-08-02 13:20:54
【问题描述】:

对于 jQuery Mobile,我需要如下标记:

<form action="..." method="get" data-ajax="false">
    <!-- Fields -->
</form>

自从我使用 Spring 以来,我真的很喜欢 &lt;form:form&gt; 为我所做的一切,包括所有方便的绑定、生成字段等。

如何让&lt;form:form&gt; 打印额外的属性?

【问题讨论】:

  • 对于 jQuery Mobile,也可以使用 target="_self" 来抑制 ajax。
  • 在这种情况下,您所说的“绑定”到底是什么意思?

标签: java jquery spring


【解决方案1】:

&lt;form:form&gt; 标签将允许任意属性。

<form:form commandName="blah" data-ajax="false">

可以正常工作。

【讨论】:

    【解决方案2】:

    您可以创建一个扩展标准 Spring 标记的自定义 JSP 标记。通过覆盖 writeOptionalAttributes 方法,您可以添加所需的附加属性。例如

    public class FormTag
        extends org.springframework.web.servlet.tags.form.FormTag {
    
        private String dataAjax;
    
        /* (non-Javadoc)
         * @see org.springframework.web.servlet.tags.form.AbstractHtmlElementTag#writeOptionalAttributes(org.springframework.web.servlet.tags.form.TagWriter)
         */
        @Override
        protected void writeOptionalAttributes(final TagWriter tagWriter) throws JspException {
            super.writeOptionalAttributes(tagWriter);
    
            writeOptionalAttribute(tagWriter, "data-ajax", getDataAjax());
        }
    
    
        /**
         * Returns the value of dataAjax
         */
        public String getDataAjax() {
            return dataAjax;
        }
    
    
        /**
         * Sets the value of dataAjax
         */
        public void setDataAjax(String dataAjax) {
            this.dataAjax = dataAjax;
        }
    
    }
    

    然后,您需要使用自定义 TLD,使 JSP 引擎可以使用新属性。我在这里只展示了它的一个 sn-p,它是 Spring 原版的复制和粘贴,只添加了你的附加属性。

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
                            "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
    <taglib>
        <tlib-version>1.0</tlib-version>
        <jsp-version>1.2</jsp-version>
        <short-name>custom-form</short-name>
        <uri>http://www.your.domain.com/tags/form</uri>
        <description>Custom Form Tag Library</description>
        <!-- <form:form/> tag -->
        <tag>
            <name>form</name>
            <tag-class>com.your.package.tag.spring.form.FormTag</tag-class>
            <body-content>JSP</body-content>
            <description>Renders an HTML 'form' tag and exposes a
                binding path to inner tags for binding.</description>
            <attribute>
                <name>id</name>
                <rtexprvalue>true</rtexprvalue>
                <description>HTML Standard Attribute</description>
            </attribute>
    ....
            <attribute>
                <name>dataAjax</name>
                <rtexprvalue>true</rtexprvalue>
                <description>jQuery data ajax attribute</description>
            </attribute>
    

    将新的 TLD 文件放入您的 Web 应用的 META-INF 目录中,然后像往常一样将其包含在您的 JSP 中

    <%@ taglib prefix="custom-form" uri="http://www.your.domain.com/tags/form" %>
    

    而不是使用

    <form:form> 
    

    使用

    <custom-form:form dataAjax="false"> 
    

    【讨论】:

      猜你喜欢
      • 2021-03-03
      • 2013-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-09
      • 1970-01-01
      • 1970-01-01
      • 2012-03-12
      相关资源
      最近更新 更多