I was not quite sure what the updateForm method does in the controller. If that is the one that loads the initial form then the model attribute name should be departments
instead of department
. These are the changes in the updateForm method.
I created an entry set which takes a hash map with the departmentId as value and divisionName as key.
Set<Map.Entry<String, String>> departments;
uiModel.addAttribute("employee", employeeService.findById(id));
List<Department> departmentsList = employeeService.getAllDepartments();
final Map<String, String> departmentsMap = new HashMap<String, String>();
if( departmentsList != null && !departmentsList.isEmpty()){
for(Department eachDepartment : departmentsList ){
if(eachDepartment != null){
departmentsMap.put(eachDepartment.getDivisionName(), eachDepartment.getDepartmentId());
}
}
}
departments = departmentsMap.entrySet();
uiModel.addAttribute("departments", departments);
Now to display this in the jsp.
<form:form modelAttribute="employee" id="employeeUpdateForm" method="post">
<form:select path="departmentId">
<form:options items="${departments}" var="department" itemValue="value" itemLabel="key"/>
</form:select>
<button type="submit">Save</button>
<button type="reset">Reset</button>
</form:form>
The value
and key
corresponds to the key value pairs of the departmentsMap
which is populated in the controller. The value gets bound to the departmentId, and the divisionName will be shown in the dropdown.
I hope this is what you want.