Spring MVC forms with select drop down list

I’m currently developing a Spring MVC application that uses forms. Some of the form elements provided are easy enough to use with a backing model (such as simple input fields (text, password, textarea)). With the selection list however I had problems since I could not work out how to populate the form from the model and how to handle the submit event. All this is probably caused by the complexity of the problem domain and the used model. Therefore I set out and implemented an easy example. There are some articles on how to do that but none with the Annotation process introduced since Spring 2.5. My example is based on this article Spring MVC dropdown example.
The main focus of this article is the contoller part because that is the only difference between the example before 2.5 and this one. The JSP part is mainly the same only that I added the action /test to the form.
The model I used is exactly the same:

public class Customer{
	 
	String country;
	String javaSkills;
 
	public String getCountry() {
		return country;
	}
	public void setCountry(String country) {
		this.country = country;
	}
	public String getJavaSkills() {
		return javaSkills;
	}
	public void setJavaSkills(String javaSkills) {
		this.javaSkills = javaSkills;
	}
	
}

Now for the interesting part – the controller – I named it TestController, the name of the controller class is irrelevant. What is important is that it is annotated with @Controller and @RequestMapping.

@Controller
@RequestMapping
public class TestController {
...

The controller needs two methods:
One to provide the data when the page is loaded and one that handles the submission of the form.
All data used in the JSP page is stored in a ModelMap that is stored in the PageContext. This is done by a method that is annotated with part of the URI. The ModelMap that is passed as an argument to this method:

@RequestMapping(value = "/test", method = RequestMethod.GET)
public String provide(ModelMap modelMap){

Next we put the backing model into the map. The path element in the JSP page corresponds to the property in the backing model. If a property has a value this is a preselected value.

Customer cust = new Customer();
cust.setJavaSkills("Spring");
modelMap.addAttribute("customerForm", cust);

The key under which the backing model is put into the ModelMap is the same as the commandName in the JSP. Next we need to define the options for the selections. This is done as a Map. the keys of the map correspond the the value attribute of the option element, the value is the displayed value.

Map<String,String> country = new LinkedHashMap<String,String>();
country.put("US", "United Stated");
country.put("CHINA", "China");
country.put("SG", "Singapore");
country.put("MY", "Malaysia");
modelMap.put("countryList", country);
 
Map<String,String> javaSkill = new LinkedHashMap<String,String>();
javaSkill.put("Hibernate", "Hibernate");
javaSkill.put("Spring", "Spring");
javaSkill.put("Apache Wicket", "Apache Wicket");
javaSkill.put("Struts", "Struts");
modelMap.put("javaSkillsList", javaSkill);

The method requires a String that represents that page to be loaded. This can either by a hard coded JSP page or a placeholder that is configured in a view.xml.

return "config_test";
     <definition name="config_test" template="/WEB-INF/views/portal/test_user.jspx"/>

For details on how to configure your application to work with view.xml see the MVC Reference.
The whole method put together looks like this:

@RequestMapping(value = "/test", method = RequestMethod.GET)
public String provide(ModelMap modelMap){
	Customer cust = new Customer();
	cust.setJavaSkills("Spring");
	modelMap.addAttribute("customerForm", cust);
	
	Map<String,String> country = new LinkedHashMap<String,String>();
	country.put("US", "United Stated");
	country.put("CHINA", "China");
	country.put("SG", "Singapore");
	country.put("MY", "Malaysia");
	modelMap.put("countryList", country);
 
	Map<String,String> javaSkill = new LinkedHashMap<String,String>();
	javaSkill.put("Hibernate", "Hibernate");
	javaSkill.put("Spring", "Spring");
	javaSkill.put("Apache Wicket", "Apache Wicket");
	javaSkill.put("Struts", "Struts");
	modelMap.put("javaSkillsList", javaSkill);
	
	return "config_test";
}

The method to handle the submission has a slightly different signature:

	@RequestMapping(value = "/test", method = RequestMethod.POST)
	public String save(@ModelAttribute("customerForm") Customer customer, ModelMap modelMap){

The request method is POST since the method in the form is POST. The model is used with the annotation @ModelAttribute and the value of the commandName. The customer instance represents the data as selected in the form. The rest of the method is straight forward and left as an exercise to the dedicated reader.

3 Gedanken zu „Spring MVC forms with select drop down list“

Schreibe einen Kommentar