「Spring 入門」@ModelAttributeアノテーションを利用するサンプル

Javaコード:
@Controller
@RequestMapping(“students")
public class StudentController {

@Autowired
StudentService studentService;

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

@RequestMapping(value = “create", method = RequestMethod.POST)
String create(@Validated StudentForm form, BindingResult result, Model model) {
if (result.hasErrors()) {
return list(model);
}
Student student = new Student();
BeanUtils.copyProperties(form, student);
studentService.create(student);
return “redirect:/students";
}
//some code
}

Java

Posted by arkgame