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>
is doing for me, with all the convenient bindings, generating fields etc.
How can I make <form:form>
print the extra attribute?
The <form:form>
tag will allow arbitrary attributes.
<form:form commandName="blah" data-ajax="false">
Will work just fine.
You could create a custom JSP tag that extends the standard Spring tag. By overriding the writeOptionalAttributes method, you can add the additional attributes you require. 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;
}
}
You then need to use a custom TLD that makes the new attribute(s) available to the JSP engine. I've only shown a snippet of it here, as its a copy & paste from the Spring original, with just your additional attribute added.
<?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 into the META-INF directory of your web app, then include it in your JSP as normal
<%@ taglib prefix="custom-form" uri="http://www.your.domain.com/tags/form" %>
And instead of using
<form:form>
use
<custom-form:form dataAjax="false">
精彩评论