Created
July 29, 2018 23:05
-
-
Save kilg-kory/0019142452642544181657c308531079 to your computer and use it in GitHub Desktop.
Recieve checkboxes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Controller | |
| @RequestMapping("/user") | |
| @PreAuthorize("hasAuthority('ADMIN')") | |
| public class UserController { | |
| @Autowired | |
| UserRepository userRepository; | |
| @GetMapping | |
| public String userList(Model model) { | |
| List<User> users = userRepository.findAll(); | |
| model.addAttribute("users", users); | |
| return "userList"; | |
| } | |
| @GetMapping("{user}") | |
| public String userEditForm(@PathVariable User user, Model model) { | |
| model.addAttribute("user", user); | |
| model.addAttribute("roles", Role.values()); | |
| return "userEdit"; | |
| } | |
| @PostMapping("{user}") | |
| public String saveUser( | |
| @PathVariable User user, | |
| @RequestParam String username, | |
| @RequestParam Set<Role> roles | |
| ) { | |
| user.setUsername(username); | |
| user.setRoles(roles); | |
| userRepository.save(user); | |
| return "redirect:/user"; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <#import "parts/common.ftl" as c> | |
| <@c.page> | |
| <p>User Edit</p> | |
| <form method="post" action="/user/${user.id}"> | |
| <input type="text" name="username" value="${user.username}"> | |
| <#list roles as role> | |
| <div> | |
| <label><input type="checkbox" | |
| name="roles" | |
| value="${role}" | |
| ${user.roles?seq_contains(role)?string("checked", "")}> ${role}</label> | |
| </div> | |
| </#list> | |
| <input type="hidden" name="_csrf" value="${_csrf.token}"> | |
| <button type="submit">Save</button> | |
| </form> | |
| <a href="/main">Main page</a> | |
| </@c.page> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment