88import javax .validation .Valid ;
99
1010import org .baeldung .spring .form .Person ;
11+ import org .baeldung .spring .validator .PersonValidator ;
12+ import org .springframework .beans .factory .annotation .Autowired ;
1113import org .springframework .stereotype .Controller ;
1214import org .springframework .ui .Model ;
1315import org .springframework .ui .ModelMap ;
2022@ Controller
2123public class PersonController {
2224
25+ @ Autowired
26+ PersonValidator validator ;
27+
2328 @ RequestMapping (value = "/person" , method = RequestMethod .GET )
2429 public ModelAndView showForm (final Model model ) {
2530
@@ -31,6 +36,8 @@ public ModelAndView showForm(final Model model) {
3136 public String submit (@ Valid @ ModelAttribute ("person" ) final Person person , final BindingResult result ,
3237 final ModelMap modelMap , final Model model ) {
3338
39+ validator .validate (person , result );
40+
3441 if (result .hasErrors ()) {
3542
3643 initData (model );
@@ -39,28 +46,28 @@ public String submit(@Valid @ModelAttribute("person") final Person person, final
3946
4047 modelMap .addAttribute ("person" , person );
4148
42- return "personResume " ;
49+ return "personView " ;
4350 }
4451
4552 private void initData (final Model model ) {
4653
47- final List <String > favouriteLanguage = new ArrayList <String >();
48- favouriteLanguage .add ("Java" );
49- favouriteLanguage .add ("C++" );
50- favouriteLanguage .add ("Perl" );
51- model .addAttribute ("favouriteLanguage " , favouriteLanguage );
52-
53- final List <String > job = new ArrayList <String >();
54- job .add ("Full time" );
55- job .add ("Part time" );
56- model .addAttribute ("job " , job );
57-
58- final Map <String , String > country = new LinkedHashMap <String , String >();
59- country .put ("US" , "United Stated" );
60- country .put ("IT" , "Italy" );
61- country .put ("UK" , "United Kingdom" );
62- country .put ("FR" , "Grance" );
63- model .addAttribute ("country " , country );
54+ final List <String > favouriteLanguageItem = new ArrayList <String >();
55+ favouriteLanguageItem .add ("Java" );
56+ favouriteLanguageItem .add ("C++" );
57+ favouriteLanguageItem .add ("Perl" );
58+ model .addAttribute ("favouriteLanguageItem " , favouriteLanguageItem );
59+
60+ final List <String > jobItem = new ArrayList <String >();
61+ jobItem .add ("Full time" );
62+ jobItem .add ("Part time" );
63+ model .addAttribute ("jobItem " , jobItem );
64+
65+ final Map <String , String > countryItems = new LinkedHashMap <String , String >();
66+ countryItems .put ("US" , "United Stated" );
67+ countryItems .put ("IT" , "Italy" );
68+ countryItems .put ("UK" , "United Kingdom" );
69+ countryItems .put ("FR" , "Grance" );
70+ model .addAttribute ("countryItems " , countryItems );
6471
6572 final List <String > fruit = new ArrayList <String >();
6673 fruit .add ("Banana" );
0 commit comments