Spring mvc miss id of dependent collection when combine form object from jsp

大憨熊 提交于 2019-12-12 10:38:00

问题


I have following controller to return view:

@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()

returns collection with ids:

On jsp I render it like this:

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

I have the follwing controller to accept this object:

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

In debug I see that terminalAdmin comes with adminRoles without ids.

How to fix this?

P.S.

It is continue of Dependent collection duplicates when I save entity


回答1:


i prefer to use Converters, because for me it's cleaner

you should have something like the following:

public class StringToAdminRoleConverter implements Converter<String, AdminRole> {

    @Autowired
    TerminalAdminService terminalAdminService;

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


来源:https://stackoverflow.com/questions/33385938/spring-mvc-miss-id-of-dependent-collection-when-combine-form-object-from-jsp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!