Home » Java » Enterprise Java » Spring MVC: form handling vol. 4 – radiobuttons

About Alexey Zvolinskiy

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 our best selling eBooks for FREE!

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

and many more ....

 

Leave a Reply

Your email address will not be published. Required fields are marked *

*


3 + one =

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

Want to take your Java Skills to the next level?
Grab our programming books for FREE!
  • Save time by leveraging our field-tested solutions to common problems.
  • The books cover a wide range of topics, from JPA and JUnit, to JMeter and Android.
  • Each book comes as a standalone guide (with source code provided), so that you use it as reference.
Last Step ...

Where should we send the free eBooks?

Good Work!
To download the books, please verify your email address by following the instructions found on the email we just sent you.