Setting a selectbox value from spring mvc

2020-04-17 04:41发布

问题:

How to set a selectbox value in jsp from controller.

Employee employee = new Employee();

I created new object for entity Employee and then set the value of designation with this code..

employee.setEmpDesignation(addEmployeeForm.getEmpDesignation());

Here is the jsp

<form:select path="empDesignation" id="emplDesignation" onchange="showTextBox();" cssClass="textBox">
    <c:forEach var="desig" items="${designation}">
        <option value="${desig.designationDesc}">
              <c:out value="${desig.designationDesc}"/>
        </option>
    </c:forEach>
</form:select>

Basically I am trying to set the values entered by the user,when error occurs.

But it always displays the first value in the selectbox.

回答1:

Why are you not using <form:options> ?

E.g:

<form:select path="empDesignation">
    <form:options items="${designation}" itemLabel="designationDesc" itemValue="designationDesc"/>
</form:select>

if your form backing object is bound, SpringMVC should handle all this for you.