Enterprise Java

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:

...
<h1>Table Reserve page</h1>
<form:form method="POST" commandname="tableReserve" action="table-reserve-result.html">
<table>
    <tbody><tr>
        <td>Smoking</td>
        <td><form:radiobutton path="smokeZone" value="yes"></form:radiobutton></td>
    </tr>
    <tr>
        <td>No Smoking</td>
        <td><form:radiobutton path="smokeZone" value="no"></form:radiobutton></td>
    </tr>
    <tr>
        <td colspan="2">
            <input value="Submit" type="submit">
        </td>
    </tr>
</tbody></table>  
</form:form>
...

And

...
<h1>Table Reserve result page</h1>
Do you choose smoking table? <br />
Answer is: [ <b>${tableReserve.smokeZone}</b> ] <br />
...

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:

...
<h1>Sport page</h1>
<form:form method="POST" commandname="sport" action="sport-result.html">
<table>
    <tbody><tr>
    <td>
	    <ul>
	    	<form:radiobuttons element="li" path="favSport" items="${sportList}">
	    </form:radiobuttons></ul>
    </td>
    </tr>
    <tr>
        <td>
            <input value="Submit" type="submit">
        </td>
    </tr>
</tbody></table>  
</form:form>
...

And

...
<h1>Sport result page</h1>
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.
 

Alexey Zvolinskiy

Alexey is a test developer with solid experience in automation of web-applications using Java, TestNG and Selenium. He is so much into QA that even after work he provides training courses for junior QA engineers.
Subscribe
Notify of
guest

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

0 Comments
Inline Feedbacks
View all comments
Back to top button