Alexey Zvolinskiy

About 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.

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.
 

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply


eight − = 7



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close