|
| 1 | +package org.baeldung.spring.controller; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.LinkedHashMap; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Map; |
| 7 | + |
| 8 | +import javax.validation.Valid; |
| 9 | + |
| 10 | +import org.baeldung.spring.form.Person; |
| 11 | +import org.baeldung.spring.validator.PersonValidator; |
| 12 | +import org.springframework.beans.factory.annotation.Autowired; |
| 13 | +import org.springframework.stereotype.Controller; |
| 14 | +import org.springframework.ui.Model; |
| 15 | +import org.springframework.ui.ModelMap; |
| 16 | +import org.springframework.validation.BindingResult; |
| 17 | +import org.springframework.web.bind.annotation.ModelAttribute; |
| 18 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 19 | +import org.springframework.web.bind.annotation.RequestMethod; |
| 20 | +import org.springframework.web.servlet.ModelAndView; |
| 21 | + |
| 22 | +@Controller |
| 23 | +public class PersonController { |
| 24 | + |
| 25 | + @Autowired |
| 26 | + PersonValidator validator; |
| 27 | + |
| 28 | + @RequestMapping(value = "/person", method = RequestMethod.GET) |
| 29 | + public ModelAndView showForm(final Model model) { |
| 30 | + |
| 31 | + initData(model); |
| 32 | + return new ModelAndView("personForm", "person", new Person()); |
| 33 | + } |
| 34 | + |
| 35 | + @RequestMapping(value = "/addPerson", method = RequestMethod.POST) |
| 36 | + public String submit(@Valid @ModelAttribute("person") final Person person, final BindingResult result, |
| 37 | + final ModelMap modelMap, final Model model) { |
| 38 | + |
| 39 | + validator.validate(person, result); |
| 40 | + |
| 41 | + if (result.hasErrors()) { |
| 42 | + |
| 43 | + initData(model); |
| 44 | + return "personForm"; |
| 45 | + } |
| 46 | + |
| 47 | + modelMap.addAttribute("person", person); |
| 48 | + |
| 49 | + return "personView"; |
| 50 | + } |
| 51 | + |
| 52 | + private void initData(final Model model) { |
| 53 | + |
| 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); |
| 71 | + |
| 72 | + final List<String> fruit = new ArrayList<String>(); |
| 73 | + fruit.add("Banana"); |
| 74 | + fruit.add("Mango"); |
| 75 | + fruit.add("Apple"); |
| 76 | + model.addAttribute("fruit", fruit); |
| 77 | + |
| 78 | + final List<String> books = new ArrayList<String>(); |
| 79 | + books.add("The Great Gatsby"); |
| 80 | + books.add("Nineteen Eighty-Four"); |
| 81 | + books.add("The Lord of the Rings"); |
| 82 | + model.addAttribute("books", books); |
| 83 | + } |
| 84 | +} |
0 commit comments