Spring has its own tag library, which makes JSPs easier to develop, maintain and read. One of these libraries is the form tag library, and in this post we’ll demonstrate how to use it.

For this we will use a sample project that will let us modify the user’s account details. In order to display the stored values for the account in the form, we have previously tied an object named foo to the session that holds all the information about our user’s account.

The form used for displaying and sending the account data is displayed here:

<form action="/account" method="post">
	<fieldset>
	<legend>Edit account information:</legend>
	<table>
		<tr>
			<td><label for="name" >Name:</label></td>
			<td><input type="text" id="name" name="name" value=${foo.name} /></td>
		</tr>
		<tr>
			<td><label for="email">E-mail:</label></td>
			<td><input type="text" id="email" name="email" value=${foo.email} /></td>
		</tr>
		<tr>
			<td><label for="website">Website:</label></td>
			<td><input type="text" id="website" name="website" value=${foo.website} /></td>
		</tr>
		<tr><td>&nbsp;</td><td><input type="submit" value="Save" /></td></tr>
	</table>
	</fieldset>
</form>

Now lets do the implementation of the form tag library on this code:

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

...

<form:form action="/account" commandName="foo">
	<fieldset>
	<legend>Account information:</legend>
	<table>
 		<tr>
			<td><label for="name" >Name:</label></td>
			<td><form:input path="name" /></td>
		</tr>
 		<tr>
			<td><label for="email">E-mail:</label></td>
			<td><form:input path="email" /></td>
		</tr>
 		<tr>
			<td><label for="website">Website:</label></td>
			<td><form:input path="website" /></td>
		</tr>
 		<tr><td>&nbsp;</td><td><input type="submit" value="Save" /></td></tr>
 	</table>
	</fieldset>
</form:form>

The Spring’s form tag is included with Spring MVC, so you’ll need to be sure to have the spring-webmvc.jar in the classpath. To use it in a JSP page, just place a directive at the top of the page as we did above:

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

As you can see from the code, we have used two tags, the form:form and the form:input.

For the form:form tag we have used two attributes:

  • action – the target URL like in a simple html form
  • commandName – name of the session object that is used for getting and setting the values

For the form:input tag:

  • path – binds the input field to a concrete property of a class

Now that we are done with the changes to the JSP page, we can move on to modify our Spring controller:

@RequestMapping("/account")
public String updateAccount (
		@ModelAttribute("foo") Foo foo,
		Model model) {

	// use fooService to save the changes contained in the foo object
	fooService.saveFoo(foo);

}

This code will run the updateAccount function when the “/account” URL is requested (once the form is submitted). This is also where Spring preforms its magic with data binding. By adding the ModelAttribute annotation to the parameter, we tell Spring to collect all the information that is available for the foo object, and to create a new object with the gathered data:

@ModelAttribute("foo") Foo foo

Once we have the foo object, we can use it save the data to the database, or do whatever the business logic requires us to do.

Posted By Vilmos Somogyi

Frond end developer and QA consultant. In this blog I'm sharing my QA experience gained over the years of working on various QA projects.

    One Response to “Binding data with Spring Forms”

  1. Discounts says:

    You can definitely see your enthusiasm within the paintings you write. The sector hopes for more passionate writers such as you who aren’t afraid to say how they believe. All the time go after your heart.

Leave a Reply




XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>