Spring MVC: form handling vol. 2 – checkbox processing

Spring MVC checkbox
It’s hard to imagine a situation when a modern web-application doesn’t have checkboxes in a form. In one of the previous posts I wrote about form handling in Spring MVC, as a continuation of this series I’m going to write about Spring MVC forms, and in particular about checkbox processing. This post will be about a standard usage of
tag.

I’ll examine three cases of the checkbox tag usage, when:

  • Chekbox is a boolean value
  • Chekbox is a string value
  • Group of checkboxes represents an array of strings

So you can see that these three points are simple enough, but they need some attention from developer’s side. Below I will describe how to realize each of these cases, you can download the source code in the end of the article.

Checkbox: boolean

SpringMVC checkbox 1
For the first case I will use the following domain model:

public class Hobby {
	
	private boolean hobbyExists;

	public boolean isHobbyExists() {
		return hobbyExists;
	}

	public void setHobbyExists(boolean hobbyExists) {
		this.hobbyExists = hobbyExists;
	}

}

This class doesn’t need any explanations, because it’s simple bean with getters and setters. Now I can show you views for this case:

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

Hobby page

Is any hobby:
...

Notice that usage of Spring form tag implies import of the appropriate tag library. The checkbox tag contains the attribute path which corresponds to the property of the domain model.
The result will be displayed on another page:

...

Hobby result page

Is any hobby? - ${hobby.hobbyExists}
...

And finally I need to declare a controller for all this stuff:

@Controller
public class HobbyController {

	@RequestMapping(value="/hobby-page")
	public ModelAndView hobbyFormPage() {
		return new ModelAndView("hobby-form", "hobby", new Hobby());
	}
	
	@RequestMapping(value="/hobby-result")
	public ModelAndView processHobby(@ModelAttribute Hobby hobby) {
		ModelAndView modelAndView = new ModelAndView("hobby-result");
		modelAndView.addObject("hobby", hobby);
		return modelAndView;
	}
	
}

Checkbox: string

SpringMVC checkbox 2
The second case will be explained with the following domain model:

public class JobOffer {
	
	private String parking;

	public String getParking() {
		return parking;
	}

	public void setParking(String parking) {
		this.parking = parking;
	}

}

The view for this situation will be a little bit different from the previous one:

...

Job Offer page

Job with parking? -
...

Notice that a new attribute appears in the checkbox tag. The value attribute represents the string property from the domain model.
The result will be displayed on another page:

...
	

Job result page

You want job with ${job.parking} You want job without parking ...

And the controller for this example:

@Controller
public class JobController {
	
	@RequestMapping(value="/job-page")
	public ModelAndView jobOfferPage() {		
		return new ModelAndView("job-form", "job", new JobOffer());
	}
	
	@RequestMapping(value="/job-result")
	public ModelAndView processJob(@ModelAttribute JobOffer jobOffer) {
		ModelAndView modelAndView = new ModelAndView("job-result");
		modelAndView.addObject("job", jobOffer);
		return modelAndView;
	}
}

Checkbox: string array

SpringMVC checkbox 3
The last case of the tutorial will describe how to bind an array of strings to a property from a domain model. In the same way you can bind values of java.util.Collection. So the domain model will look like this:

public class Food {
	
	private String[] favoriteFruit;

	public String[] getFavoriteFruit() {
		return favoriteFruit;
	}

	public void setFavoriteFruit(String[] favoriteFruit) {
		this.favoriteFruit = favoriteFruit;
	}
}

The page with the form:

...

Fruit page

Apple
Orange
Banana
...

The values from the checkbox tags will be passed to the string array during the processing.
The result page:

...

Food result page

You prefer:
${fruit}
...

And the controller for the last case:

@Controller
public class FoodController {

	@RequestMapping(value="/food-page")
	public ModelAndView foodPage() {		
		return new ModelAndView("food-form", "food", new Food());
	}
	
	@RequestMapping(value="/food-result")
	public ModelAndView processFuits(@ModelAttribute Food food) {
		ModelAndView modelAndView = new ModelAndView("food-result");
		modelAndView.addObject("food", food);
		return modelAndView;
	}
	
}

Checkbox tag feature

When some page with form is rendered, you can open its source code, and see something like this:

...


...

Strange, where does the additional input appear from? Official documentation explains this fact:

When a checkbox in an HTML page is not checked, its value will not be sent to the server as part of the HTTP request parameters once the form is submitted, so we need a workaround for this quirk in HTML in order for Spring form data binding to work. The checkbox tag follows the existing Spring convention of including a hidden parameter prefixed by an underscore (“_”) for each checkbox. By doing this, you are effectively telling Spring that “ the checkbox was visible in the form and I want my object to which the form data will be bound to reflect the state of the checkbox no matter what ”.

Summary

This article gives you a high level overview of the checkbox tag usage, but this is just a base. In the nearest posts I will show you more interesting situations of the checkbox usage in Spring MVC.
You can download the source code of the entire application clicking on the link.

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS
  • bphilipnyc

    Nice and simple intro. Thanks. Think you mean c:forEach