Skip to main content

Development

Interpreting Spring form tags

Spring MVC provides tags for handling form elements when using JSP.

Each spring form tag will generate HTML tags at runtime. These tags provide support for the set of attributes of its corresponding HTML tag.

This blog provides a quick reference of how to interpret some of these commonly used spring form tags and understand the attribute level mappings between the Spring form tags and its HTML Counterpart. To use Spring form tags in your JSP, include this library:

<%@ taglib prefix=”form” uri=”http://www.springframework.org/tags/form” %>

The prefix ‘form’ will be used to precede the Spring form tag names. Spring form tags are only valid when contained with the Spring Form tag:

<form:form>   …   </form:form>

Spring tag

HTML Tag

Additional Info

<form:form> <form method=”POST”>
  • exposes a binding path to inner tags for binding
  • puts the command object in the PageContext
<form:input path=”firstName” /> <input name=”firstName” type=”text” value=”abc”/>
  • “path” <=> “name”
  • Spring path value is set to the html name attribute. So, Setting the name attribute explicitly to form:input tag has no effect and is simply ignored.
  • “path” attribute binds the field to the model
<form:select path=”examples” items=”${examples}”/> <td><select name=”examples” multiple=”true”> <option value=”example1″>Example 1</option> <option value=”example2″ selected=”selected”>Example 2</option> </select> </td>
  • supports data binding to the selected option as well as the use of nested option and options tags
<form:hidden path=”hiddenVar” /> <input name=”hiddenVar” type=”hidden” value=”hiddenVal”/>
  • “path” <=> “name”
  • Spring path value is set to the html name attribute. So, Setting the name attribute explicitly to form:input tag has no effect and is simply ignored.
  • “path” attribute binds the field to the model

References: Spring Form TLD reference:

http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/spring-form.tld.html

Spring Framework Web MVC:

http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/view.html#view-jsp-formtaglib

Thoughts on “Interpreting Spring form tags”

  1. Hi Saraswathy, its a good post on Spring topic. I am into software testing side and new to here. Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Saraswathy Kalyani

More from this Author

Categories
Follow Us