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

...
<h1>Table Reserve page</h1>
<form:form method="POST" commandName="tableReserve" action="table-reserve-result.html">
<table>
    <tr>
        <td>Smoking</td>
        <td><form:radiobutton path="smokeZone" value="yes" /></td>
    </tr>
    <tr>
        <td>No Smoking</td>
        <td><form:radiobutton path="smokeZone" value="no" /></td>
    </tr>
    <tr>
        <td colspan="2">
            <input type="submit" value="Submit"/>
        </td>
    </tr>
</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<String> sportList = new ArrayList<String>();
		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

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

About The Author

Mathematician, programmer, wrestler, last action hero... Java / Scala architect, trainer, entrepreneur, author of this blog

Close