2015-10-28 108 views
7

結合形式對象我有以下控制器返回視圖:與IDSSpring MVC的思念ID時,從JSP

@RequestMapping(value = "/admin/adminUsers", method = RequestMethod.GET) 
    public String adminUsers(ModelMap model, HttpSession session) { 
     Set<TerminalAdmin> users = terminalAdminService.getAllAdmins(); 
     session.setAttribute("users", users); 
     model.addAttribute("adminRoles", terminalAdminService.findAllAdminRoles()); 
     model.addAttribute("terminalAdmin", new TerminalAdmin()); 
     model.addAttribute("generatedPassword", PasswordUpdateStatus.generatePassword()); 
     return "admin/adminUsers"; 
    } 

terminalAdminService.findAllAdminRoles() 

回報集合:

enter image description here

JSP的我使其像這樣:

<form:form modelAttribute="terminalAdmin" action="/admin/addNewAdmin"> 
    ... 
    <form:checkboxes items="${adminRoles}" path="adminRoles"/> 
    ... 
</form:form> 

我有follwing控制器接受此對象:

@RequestMapping(value = "/admin/addNewAdmin") 
    public String adminUsers(@ModelAttribute @Valid TerminalAdmin terminalAdmin...){ 
     .... 
} 

在調試中,我看到terminalAdmin帶有沒有ID的adminRoles。

enter image description here

如何解決這一問題?

P.S.

它繼續Dependent collection duplicates when I save entity

+0

你看到的ID在html的jsp頁面? (在下拉菜單中)? –

+0

這個API你用於持久性? JPA? –

+0

@與此問題無關的JPA – gstackoverflow

回答

1

我更喜歡使用Converters,因爲對我來說這是清潔

你應該有類似以下內容:

public class StringToAdminRoleConverter implements Converter<String, AdminRole> { 

    @Autowired 
    TerminalAdminService terminalAdminService; 

    @Override 
    public AdminRole convert(String role) { 
     return terminalAdminService.findRoleByName(role); 
    } 
}