For binding data on a jsp page to a form, one of the easiest ways is using spring form tag.
To generate a radio button, we use this syntax:
To generate a list of radio button, we use this syntax:
As you see, the lstSkin is a Map, the key of Map is radio value and the value of Map is radio label
References:
In this article, I will show you how to bind form data by using:
- <form:radiobutton>
- <form:radiobuttons>
1. Form Bean
public class User { // a radio private String sex; // radios private String skin; .... }
2. Form Tags
To generate a radio button, we use this syntax:
<form:radiobutton path="sex" value="M" />
- path="sex" means the selected radio value binds to sex attribute
- value="M" the label of this radio
To generate a list of radio button, we use this syntax:
<form:radiobuttons
path="skin"
items="${lstSkin}"
/>
- path="skin" means the selected radio value binds to sex attribute
- items="${lstSkin}" means the radio value and radio lable are stored in lstSkin
4. In Controller
We initialize the item list in GET method@RequestMapping(value = "/modifyUser", method = RequestMethod.GET) public ModelAndView modifyUser() { Map<String, String> lstSkin = new HashMap<String, String>(); lstSkin.put("1", "White"); lstSkin.put("2", "Black"); lstSkin.put("3", "Brown"); lstSkin.put("4", "Orange"); ModelAndView modelView = new ModelAndView("modifyUser"); modelView.addObject("lstSkin", lstSkin); modelView.addObject("user", this.user); return modelView; }
As you see, the lstSkin is a Map, the key of Map is radio value and the value of Map is radio label
Note
The order of items is not guaranteed in JSP
The order of items is not guaranteed in JSP
References:
No comments:
Post a Comment