Generifing MVC spring select lists

In my previous article I have shown how to create a simple form with two select boxes using spring form from MVC. When we have an application with may forms we want to be able to use a general model for backing the data. We also might want to have certain elements multiple times, e.g. not one Customer but several. We will take theses step by step.

Multiple Customers

The first step is to change the backing model to a list and iterate over all the elements.

Creating the backing model

This step is rather straight forward: Create a class that contains a List of type Customer and add the appropriate getter and setter methods:

public class CustomerList{
	private List<Customer> list;

	public List<Customer> getList() {
		return list;

	public void setList(List<Customer> list) {
		this.list = list;

Adjusting the controller

Instead of a Customer instance we put a CustomerList instance into the map. To make the whole list a bit more interessting each customer in the list should have a different preselected skill:

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

	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);
	List<String> l = new ArrayList<String>();
	l.add("Apache Wicket");
	Random rand = new Random(System.nanoTime());
	List<Customer> list = new ArrayList<Customer>();
	for (int i=0;i<4;i++){
		Customer cust = new Customer();
		String skill = l.get(rand.nextInt(4));
	CustomerList cl = new CustomerList();
	modelMap.addAttribute("customerForm", cl);	
	return "config_test";

Applying changes to the JSP

In the previous example we had a table with two rows; one for the county selection and one for the skill. Since our model is now a list of Customers (4 to be precise) we have to add these two rows four times. We do that by iterating through the list. The essential point is that we define a variable to store the current status. We will need this status to access the correct element in each iteration process.
In the select elements the path is augmented: it is now the list at the index and then the appropriate field:

<form:form method="POST" commandName="customerForm" action="${link_url_home_config_test_save}">
	 	<c:forEach items="${customerForm.list }" var="customer" varStatus="x">
			<td>Country : </td>
				<form:select path="list[${x.index}].country">
					<form:option value="NONE" label="--- Select ---"/>
					<form:options items="${countryList}" />
			<td>Java Skills : </td>
				<form:select path="list[${x.index}].javaSkills" items="${javaSkillsList}" multiple="true" />
			<td colspan="3"><input type="submit" /></td>

Ein Gedanke zu „Generifing MVC spring select lists“

Schreibe einen Kommentar