È possibile creare un tag JSP personalizzato che estenda il tag Spring standard. Sovrascrivendo il metodo writeOptionalAttributes, è possibile aggiungere gli attributi aggiuntivi richiesti. Per esempio
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;
}
}
È quindi necessario utilizzare un TLD personalizzata che rende il nuovo attributo (s) a disposizione del motore di JSP. Ne ho solo mostrato uno snippet qui, dato che è una copia & incolla dell'originale Spring, con solo il tuo attributo aggiuntivo aggiunto.
<?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>
mettere il nuovo file TLD nella directory META-INF del vostro web app, quindi includere nel vostro JSP come normale
<%@ taglib prefix="custom-form" uri="http://www.your.domain.com/tags/form" %>
E invece di utilizzare
<form:form>
uso
<custom-form:form dataAjax="false">
fonte
2011-08-11 07:13:56
Per jQuery Mobile è anche possibile sopprimere ajax con target = "_ self". –