简体   繁体   中英

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

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?

PS

It is continue of Dependent collection duplicates when I save entity

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM