|
3 | 3 | import model.SimpleModel; |
4 | 4 | import org.springframework.stereotype.Controller; |
5 | 5 | import org.springframework.ui.Model; |
| 6 | +import org.springframework.validation.DataBinder; |
6 | 7 | import org.springframework.validation.annotation.Validated; |
| 8 | +import org.springframework.web.bind.annotation.InitBinder; |
| 9 | +import org.springframework.web.bind.annotation.RequestBody; |
7 | 10 | import org.springframework.web.bind.annotation.RequestMapping; |
| 11 | +import org.springframework.web.bind.annotation.RequestMethod; |
| 12 | +import validator.SimpleModelValidator; |
8 | 13 |
|
9 | 14 | /** |
10 | 15 | * 简单的Spring {@link org.springframework.stereotype.Controller}. |
|
14 | 19 | @Controller |
15 | 20 | public class SimpleController { |
16 | 21 |
|
| 22 | + @InitBinder |
| 23 | + public void initBinder(DataBinder dataBinder) { |
| 24 | + //dataBinder.setValidator(new SimpleModelValidator()); |
| 25 | + dataBinder.addValidators(new SimpleModelValidator()); |
| 26 | + } |
| 27 | + |
17 | 28 | @RequestMapping("/echo") |
18 | 29 | public String echo(String name, Model model) { |
19 | 30 | model.addAttribute("echo", "hello " + name); |
20 | 31 | return "echo"; |
21 | 32 | } |
22 | 33 |
|
23 | | - @RequestMapping("/echoAgain") |
24 | | - public String echo(@Validated SimpleModel simpleModel, Model model) { |
| 34 | + @RequestMapping(value = "/echoAgain", method = RequestMethod.POST) |
| 35 | + public String echo(@Validated @RequestBody SimpleModel simpleModel, Model model) { |
25 | 36 | model.addAttribute("echo", "hello " + simpleModel.getName() + ", your age is " + simpleModel.getAge() + "."); |
26 | 37 | System.out.println(model.asMap().get("simpleModel")); |
27 | 38 | return "echo"; |
|
0 commit comments