Additional attributes from Spring <form: form>

For jQuery Mobile I need markup like:

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

Since I work with Spring, I really like what <form:form> does for me, with all the convenient links, generating fields, etc.

How can I make <form:form> print an additional attribute?

+7
source share
2 answers

The <form:form> allows for arbitrary attributes.

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

Will work fine.

+4
source

You can create your own JSP tag that extends the standard Spring tag. By overriding the writeOptionalAttributes method, you can add additional attributes you need. for example

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

Then you need to use a custom TLD that makes the new attributes available to the JSP engine. I just showed the fragment here, as a copy and paste from the original Spring, adding only the added additional attribute.

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

put the new TLD file in the META-INF directory of your web application, then include it in your JSP as usual

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

And instead of using

 <form:form> 

use

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

All Articles