Advertisement
Guest User

Untitled

a guest
Aug 18th, 2015
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. @Controller
  2. public class ContactController {
  3.  
  4. private static List<Contact> contacts = new ArrayList<>();
  5.  
  6. static {
  7. contacts.add(new Contact("Barack", "Obama", "barack.o@wh.com", "147-852-965"));
  8. contacts.add(new Contact("George", "Bush", "george.b@wh.com", "785-985-652"));
  9. contacts.add(new Contact("Bill", "Clinton", "bill.c@wh.com", "236-587-412"));
  10. contacts.add(new Contact("Ronald", "Reagan", "ronald.r@wh.com", "369-852-452"));
  11. }
  12.  
  13. @RequestMapping(value = "hello", method = RequestMethod.GET)
  14. public String get(Model model) {
  15.  
  16. ContactForm contactForm = new ContactForm();
  17. contactForm.setContacts(contacts);
  18. model.addAttribute( "contactForm", contactForm);
  19. return "add_contact";
  20. }
  21.  
  22. @RequestMapping(value = "/save", method = RequestMethod.POST)
  23. public String save(@ModelAttribute ContactForm contactForm, Model model) {
  24.  
  25. List<Contact> contactList = contactForm.getContacts();
  26. if(contactList != null && contactList.size() > 0) {
  27. contacts = contactList;
  28. }
  29.  
  30. return "show_contact";
  31. }
  32. }
  33.  
  34. @Configuration
  35. @EnableWebMvc
  36. @ComponentScan("controller")
  37. public class WebConfig extends WebMvcConfigurerAdapter {
  38.  
  39. @Bean
  40. public ViewResolver viewResolver() {
  41.  
  42. InternalResourceViewResolver resolver
  43. = new InternalResourceViewResolver();
  44. resolver.setPrefix("/WEB-INF/jsp/");
  45. resolver.setSuffix(".jsp");
  46. return resolver;
  47.  
  48. }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement