Monday, February 18, 2013

SpringMVC Form Tag Lib Select + Option

For binding data on a jsp page to a form, one of the easiest ways is using spring form tag.

In this article, I will show you how to bind form data by using:
  • <form:select>
  • <form:option>
  • <form:options>

1. Option object

This object contain a pair value of each item in combo box or list box. It composes of item value and item label.

public class Option {

    private String  itemValue;

    private String  itemLabel;
...
}

2. Form Bean


public class User {

    // for a dropdown by using select tag
    private String skill1;

    // for a dropdown by using select, option and options tag
    private String skill2;

    // for multi select list box
    private String[] skills;
....
}

As above code, the attribute skills is used in binding a multi select list box.

3. Form Tags


To generate a dropdown box, we use this syntax:
<form:select 
    path="skill1" 
    items="${lstSkill}" 
    itemValue="itemValue" 
    itemLabel="itemLabel" 
/> 

  • path="skill1" means the selected value binds to skill1 attribute
  • items="${lstSkill}" means the items displayed in dropdown is stored in lstSkill
  • itemValue="itemValue" means the value of each item comes from field itemValue of Option object
  • itemLabel="itemLabel" means the label of each item is stored in field itemLabel of Option object

Another way to generate a combo box:
<form:select path="skill2">
    <form:option value="-" label="--Please Select"/>
    <form:options path="skill2" items="${lstSkill}" itemValue="itemValue" itemLabel="itemLabel" />
</form:select>


To generate a multi select list box, we use this syntax:
<form:select 
    path="skills"     
    multiple="true" 
    items="${lstSkill}" 
    itemValue="itemValue" 
    itemLabel="itemLabel" 
/>

  • path="skills", here the skills is an array of string
  • multiple="true"

4. In Controller

We initialize the item list in GET method
@RequestMapping(value = "/modifyUser", method = RequestMethod.GET)
public ModelAndView modifyUser() {

        /* We use a list of Option instead of Map */
        List<Option> lstSkill = new ArrayList<Option>();
 
        lstSkill.add(new Option("1", "Java"));
        lstSkill.add(new Option("2", "C#"));
        lstSkill.add(new Option("3", "PHP"));
        lstSkill.add(new Option("4", "C++"));
  
        ModelAndView modelView = new ModelAndView("modifyUser");
        modelView.addObject("lstSkill", lstSkill);
        modelView.addObject("user", this.user);
        return modelView;
} 

References:
Source code:
  • For the demonstration, please download source code from HERE
  • My repository on Github

3 comments:

  1. sir i want to one jsp page and select all the dropdown and text field then click the ok button after that call the doa class then execute and gating the output ..what ever wrote the query and view the data plz help me...

    ReplyDelete
  2. Jackpotcity Casino - Mapyro
    Welcome to Jackpotcity Casino. We have a large 세종특별자치 출장마사지 variety 과천 출장샵 of games, a good 전라남도 출장마사지 variety of slots, a good number 거제 출장안마 of 부산광역 출장마사지 tables, and a very good casino.

    ReplyDelete