|
| 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.springframework.stereotype.Controller; |
| 12 | +import org.springframework.ui.Model; |
| 13 | +import org.springframework.ui.ModelMap; |
| 14 | +import org.springframework.validation.BindingResult; |
| 15 | +import org.springframework.web.bind.annotation.ModelAttribute; |
| 16 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 17 | +import org.springframework.web.bind.annotation.RequestMethod; |
| 18 | +import org.springframework.web.servlet.ModelAndView; |
| 19 | + |
| 20 | +@Controller |
| 21 | +public class PersonController { |
| 22 | + |
| 23 | + @RequestMapping(value = "/person", method = RequestMethod.GET) |
| 24 | + public ModelAndView initForm(final Model model) { |
| 25 | + |
| 26 | + final List<String> favouriteLanguage = new ArrayList<String>(); |
| 27 | + favouriteLanguage.add("Java"); |
| 28 | + favouriteLanguage.add("C++"); |
| 29 | + favouriteLanguage.add("Perl"); |
| 30 | + model.addAttribute("favouriteLanguage", favouriteLanguage); |
| 31 | + |
| 32 | + final List<String> job = new ArrayList<String>(); |
| 33 | + job.add("Full time"); |
| 34 | + job.add("Part time"); |
| 35 | + model.addAttribute("job", job); |
| 36 | + |
| 37 | + final Map<String, String> country = new LinkedHashMap<String, String>(); |
| 38 | + country.put("US", "United Stated"); |
| 39 | + country.put("IT", "Italy"); |
| 40 | + country.put("UK", "United Kingdom"); |
| 41 | + country.put("FR", "Grance"); |
| 42 | + model.addAttribute("country", country); |
| 43 | + |
| 44 | + final List<String> fruit = new ArrayList<String>(); |
| 45 | + fruit.add("Banana"); |
| 46 | + fruit.add("Mango"); |
| 47 | + fruit.add("Apple"); |
| 48 | + model.addAttribute("fruit", fruit); |
| 49 | + |
| 50 | + final List<String> books = new ArrayList<String>(); |
| 51 | + books.add("The Great Gatsby"); |
| 52 | + books.add("Nineteen Eighty-Four"); |
| 53 | + books.add("The Lord of the Rings"); |
| 54 | + model.addAttribute("books", books); |
| 55 | + |
| 56 | + return new ModelAndView("personForm", "person", new Person()); |
| 57 | + } |
| 58 | + |
| 59 | + @RequestMapping(value = "/addPerson", method = RequestMethod.POST) |
| 60 | + public String submit(@Valid @ModelAttribute("person") final Person person, final BindingResult result, |
| 61 | + final ModelMap model) { |
| 62 | + |
| 63 | + if (result.hasErrors()) { |
| 64 | + return "error"; |
| 65 | + } |
| 66 | + |
| 67 | + model.addAttribute("person", person); |
| 68 | + |
| 69 | + return "personResume"; |
| 70 | + } |
| 71 | + // |
| 72 | + // protected Map<String, List<String>> referenceData(final |
| 73 | + // HttpServletRequest request) throws Exception { |
| 74 | + // |
| 75 | + // final Map<String, List<String>> referenceData = new HashMap<>(); |
| 76 | + // |
| 77 | + // final List<String> favouriteLanguageList = new ArrayList<String>(); |
| 78 | + // favouriteLanguageList.add("Java"); |
| 79 | + // favouriteLanguageList.add("C++"); |
| 80 | + // favouriteLanguageList.add("Perl"); |
| 81 | + // referenceData.put("favouriteLanguageList", favouriteLanguageList); |
| 82 | + // |
| 83 | + // return referenceData; |
| 84 | + // |
| 85 | + // } |
| 86 | +} |
0 commit comments