Setting a selectbox value from spring mvc

2020-04-17 04:34发布

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条回答
仙女界的扛把子
2楼-- · 2020-04-17 05:12

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.

查看更多
登录 后发表回答