Binding data to a drop-down box in Spring MVC

I have a simple spring mvc web application. I want to associate a list with a drop down list. In this case, the list items are usually attached to the dropdown; but if I select an item and click the "Submit" button, it always passes "0" instead of a value.

This is my controller.

public EditEmployeeController() {
        setCommandClass(Employee.class);
        setCommandName("employee");
}

@Override
protected Map referenceData(HttpServletRequest request) throws Exception {
    Map referenceData = new HashMap();        
    List roleList = roleService.listRoles();
    referenceData.put("roleList", roleList);
    return referenceData;
}

@Override
protected ModelAndView onSubmit(HttpServletRequest request,
        HttpServletResponse response,
        Object command,
        BindException errors) throws Exception {
    Employee employee = (Employee) command;
    employeeService.updateEmployee(employee);
    return new ModelAndView(getSuccessView());
}

This is my view (jsp)

<c:nestedPath path="employee">
            <form method="post" action="./EditEmployee.htm">
........
.......

Select Role :                
<c:bind path="roleID">
 <select name="roleLists">
   <s:forEach items="${roleList}" var="role" >
       <option value="<s:out value="${role.roleID}"/>" <s:if test="${role.roleID == status.value}"> selected="selected"</s:if>><s:out value="${role.title}"/></option>
        </s:forEach>
  </select>
</c:bind>
<input type="submit" value="Update employee"/
</form>
</c:nestedPath>

Any idea to solve my problem .. ??

Thanks in advance!

+3
source share
1 answer

Use Spring Form Tag

add <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>to jsp

and use the code below

<form:select path="role"  id="role" >
    <form:options items="${roleList}" itemValue="roleID" itemLabel="title"/>
</form:select>
+3
source

Source: https://habr.com/ru/post/1768513/


All Articles