1

The below code is for single value selection which is working absolutely fine. But when I add attribue multiple="true" to form:select tag, then country object parameter in save() method is having null value when I select multiple value in Jsp. What will be the change that I have to do in jsp so that I can get the list of country objects in controller method. Please advise.

JSP Code

<form:form modelAttribute="country">    
                        <li>
                            <form:label cssClass="mandatory" path="state">Select State</form:label>
                            <form:select path="state" cssErrorClass="error">
                            <form:option value="" label="Please Select..." />
                            <!-- Collection<State> listOfStates; This value holds the list of State objects by using controller method.--> 
                            <c:forEach items="${listOfStates}" var="s">
                            <form:option value="${s.stateUniqueCode}" label="${s.stateName}"/>  
                            </c:forEach>
                            </form:select>
                        </li>
                        </form>

Controller method

@RequestMapping(params = "_save", method = RequestMethod.POST)
                public ModelAndView save(@ModelAttribute("country") Country country, BindingResult result, SessionStatus status) {

            //some busines logic....
            return new ModelAndView(mavString.toString());
            }

Country and state classes _________________________

 /** Country object**/
    public class Country{
    private State state;
    //getter and setters
    }

    /** State object**/
    public class State{
        private long stateUniqueCode;
        private String stateName;
    //getter and setters
    }

If I convert country variable to List country in Country class. How should be the jsp and controller method be changed.

  • check my answer [here](http://stackoverflow.com/questions/25888668/dynamic-drop-down-loaded-with-ajax-is-found-as-null-in-spring-mvc-controller/43540209#43540209). May be it'll be help you – Shakhboz Apr 21 '17 at 10:36

1 Answers1

0

As per given jsp your model(Java classes of Country and state should be as follows)

public class Country{
private List<State> stateList;
//getter and setters
}

/** State object**/
public class State{
    private long stateUniqueCode;
    private String stateName;
//getter and setters
}

and you should bind stateList attribute in your path as follows.

<form:form modelAttribute="country">    
<li>
    <form:label cssClass="mandatory" path="stateList">Select State</form:label>
        <form:select path="stateList" cssErrorClass="error">
             <form:option value="" label="Please Select..." />
                <c:forEach items="${listOfStates}" var="s">
                    <form:option value="${s.stateUniqueCode}" label="${s.stateName}"/>  
                </c:forEach>
        </form:select>
</li>