2013-06-12 5 views
0

에서 드롭 다운 목록을 채우기 것은 까다로운 문제입니다, 나는 다음하여 jspx 있습니다스프링 MVC. 여기에 다른 개체

<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> 

내 updateForm 방법 :

@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"; 
} 

"부서는"두 개의 필드가 있습니다 departmentId (INT)와 divisionName를 (끈).

"employee"와 "department"는 두 개의 서로 다른 객체이므로 "department"의 문자열 표현으로 "employee"(departmentId)와 관련된 필드를 채우고 싶습니다. 그들의 부서 ID가 서로 일치합니다. 특정 부서가 선택되면이 ID는 employee.departmentId에 저장됩니다.

미리 감사드립니다.

+0

몇 가지 문제가 있습니까? 오류 또는 문제는 무엇입니까? – Usha

+0

문제는 어떻게 수행해야할지 모르겠다. 문제는 – orionix

답변

3

컨트롤러에서 updateForm 메서드가 수행하는 작업이 확실하지 않았습니다. 그것이 초기 양식을로드하는 모델이면 모델 속성 이름은 department 대신 departments이어야합니다. 이것은 updateForm 메소드의 변경 사항입니다. 값으로 departmentId, 부분 이름으로 divisionName을 키로 사용하여 해시 맵을 사용하는 엔트리 세트를 만들었습니다.

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); 

이제 이것을 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> 

valuekey 컨트롤러 채워되는 departmentsMap의 키 값의 쌍에 대응한다. 값은 departmentId에 바인딩되고 divisionName은 드롭 다운에 표시됩니다.

나는 이것이 당신이 원하는 바란다.

+0

예, 도움이되었습니다. 귀하의 제안은 절대적이며 의심 할 여지없이 적합합니다! 내가 찾고 있었던 것이 었습니다! 감사합니다. departmentsMap = new HashMap () - departmentId가 Long이기 때문에 : > departments를 설정하고 마지막 Map을 설정합니다. – orionix