[Spring]@ModelAttributeで画面の入力値を受け取るサンプル

書式
@ModelAttribute
Form名 setUpForm
使用例

@Controller
@RequestMapping("users")
public class UserController {

    @Autowired
    UserService userrService;

    @ModelAttribute
    UserForm setUpForm() {
        return new UserForm();
    }


    @RequestMapping(value = "update", method = RequestMethod.POST)
    String update(@Validated UserForm form, BindingResult result, Model model) {
        if (result.hasErrors()) {
            return list(model);
        }
        User user = new User();
        BeanUtils.copyProperties(form, user);
        userService.update(user);
        return "redirect:/users";
    }
}

 

SpringMVC

Posted by arkgame