«Back

Spring form tags

Graig Walls talks about new Spring Form tags in his Spring-Loaded blog. His talking about how cumbersome the spring:bind tag is and how the new form tags make life easier. The form tags are like struts form tags.

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<form:form commandName="command"book>
...
<form:input path="author"/>
...
</form>

There is tags for each form field type such as

  • <form:input>
  • <form:password>
  • <form:hidden>
  • <form:select>
  • ...

Now I have to disagree with Craig. I have developed numerous applications with struts and those tags were many times really inconvinient and inflexiable. At first the spring:bind tag felt tidious because of repeated code but then came JSP 2.0 to rescue. With JSP 2.0 and the tagdir attribute in taglib definition really helped. Now I could create tags without creating actually taglib. I have a sample implementation for most common form tags I use in spring mvc applications. All I need to do is copy the files to my webapp project and if I need a different look for my forms I just change them. No tidious taglib java code changes just some EL and maybe some ready made taglibs such as spring:bind. Below is a small example form. See no spring:bind here

<%@ taglib prefix="html" tagdir="/WEB-INF/tags/html" %>
<form action="<portlet:actionURL/>">
Author: <html:input path="book.author" size="30" maxlength="80"/>
</form> 

Now in /WEB-INF/tags/html I have a file named input.tag which has the spring:bind tag along with possible error message displaying:

<%@ tag dynamic-attributes="attributes" isELIgnored="false" body-content="empty" %>
<%@ include file="taglibs.jsp" %>
<%@ attribute name="path" required="true" %>
<spring:bind path="${path}">
<html:attributes var="attrString" attributeMap="${attributes}" type="text" name="${status.expression}" value="${status.value}">
<input ${attrString} />
</html:attributes>
<span style="color:#A00000">${status.errorMessage}</span>
</spring:bind>

See how easy it is. The good news is that spring:bind is not deprecated.

Previous
Comments
No comments yet. sign-in-to-comment