Spring MVC: form handling vol. 4 – radiobuttons

In the world of software development radiobuttons are essential part of graphic user interface. Spring MVC tag library provides special tag for this element of form. If to be more precisely there are two tags for this purpose: radiobutton, radiobuttons. Both of them are useful for a particular task. As you understand I’m going to show how to use these tags in the post.

This tutorial will not be difficult for you if you familiar with my previous tutorials about Spring MVC tag library, in particular work with checkbox and checkboxes tags. Most part of explanation for the radiobutton (-s) tag will be redundant since everything works as in examples with checkbox (-es) tag.
Full version of the code in this post you can find on GitHub.

Radiobutton: string

In this section I’m going to examine case when radiobutton tag will be used multiple times on the same page. The result will be represented as a string object.
Here is a POJO for this example:

public class TableReserve {
	
	private String smokeZone;

	public String getSmokeZone() {
		return smokeZone;
	}

	public void setSmokeZone(String smokeZone) {
		this.smokeZone = smokeZone;
	}	

}

And here is a controller which will process operations related to the table reservation:

@Controller
public class TableReserveController {
	
	@RequestMapping(value="/table-reserve-page")
	public ModelAndView tableReservePage() {
		return new ModelAndView("table-reserve-form", "tableReserve", new TableReserve());		
	}
	
	@RequestMapping(value="/table-reserve-result")
	public ModelAndView processTableReserve(@ModelAttribute TableReserve tableReserve) {
		ModelAndView mav = new ModelAndView("table-reserve-result");
		mav.addObject("tableReserve", tableReserve);
		return mav;
	}
	
}

In the controller everything is as usually in the same situations. If you don’t know at all how Spring MVC controller can interact with form you can read my article about form handling.

And here are code snippets of appropriate views:

Spring-MVC-radiobutton-String

...

Table Reserve page

Smoking
No Smoking
...

And

...

Table Reserve result page

Do you choose smoking table?
Answer is: [ ${tableReserve.smokeZone} ]
...

That’s it for the first example.

Radiobuttons: string

In this section I’m going to consider situation when I need to generate values for radiobuttons at runtime. For this purpose I will add all values for radiobuttons in java.util.List object (as with checkboxes tag you can use simple array or java.util.Map object).

Here is a POJO for the second example:

public class Sport {
	
	private String favSport;

	public String getFavSport() {
		return favSport;
	}

	public void setFavSport(String favSport) {
		this.favSport = favSport;
	}

}

Appropriate controller:

@Controller
public class SportController {

	@RequestMapping(value="/sport-page")
	private ModelAndView footballPage() {
		ModelAndView mav = new ModelAndView("sport-form");
		
		List sportList = new ArrayList();
		sportList.add("Judo");
		sportList.add("Basketball");
		sportList.add("Ping-Pong");
		
		mav.addObject("sportList", sportList);
		mav.addObject("sport", new Sport());
		
		return mav;
	}
	
	@RequestMapping(value="/sport-result")
	private ModelAndView processTeams(@ModelAttribute Sport sport) {
		ModelAndView mav = new ModelAndView("sport-result");
		mav.addObject("sport", sport);
		return mav;
	}
	
}

As you understand, sportList contains values which I decided to generate at runtime.

Views:

Spring-MVC-radiobutton-List

...

Sport page

...

And

...

Sport result page

Your favorite sport is: ${sport.favSport} ...

Summary

In this way you can add radiobuttons in your form, which you are processing with the Spring MVC. The tag library provides simple decision for implementation of all form controls. All you need is to create a form, corresponding POJO, bind it together and that’s it.

Share and Enjoy

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