0

Here is tricky issue, I have next jspx:

<form:form modelAttribute="employee" id="employeeUpdateForm" method="post">
    <form:select path="departmentId">           
    <form:options items="${departments}" />
</form:select>

<button type="submit">Save</button>
    <button type="reset">Reset</button>
</form:form>

and my updateForm method:

@RequestMapping(value = "/{id}", params = "form", method = RequestMethod.GET) 
public String updateForm(@PathVariable ("id") Long id, Model uiModel) { 
uiModel.addAttribute("employee", employeeService.findById(id));

List<Department> departments = employeeService.getAllDepartments();
uiModel.addAttribute("department", departments);

return "staff/update";
}

"department" has two fields: departmentId (int) and divisionName (String).

So, "employee" and "department" are two different objects, I would like to have ability to populate field related to "employee" (departmentId) with string representations from "department". Their departmentId match one another. Once certain department is chosen its id is putting to employee.departmentId.

Thanks in advance!

2
  • Are you facing some problems? Whats the error or issue? Commented Jun 12, 2013 at 16:55
  • The problem is I"m not sure how to perform it. That is the issue Commented Jun 12, 2013 at 17:05

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, you have helped me. Your proposition is absolutely and undoubtedly suitable! It is what I have been looking for! Thank you Usha. Just small remark: Set<Map.Entry<String, Long>> departments; and final Map<String, Long> departmentsMap = new HashMap<String, Long>() - since departmentId is Long.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.