| src/main/java/info/ahlberg/spring/controller/ContactController.java |
| 1 | 1 | package info.ahlberg.spring.controller; |
| 2 | | import java.util.List; |
| 3 | | |
| 4 | 2 | import info.ahlberg.spring.domain.Contact; |
| 5 | 3 | import info.ahlberg.spring.service.ContactService; |
| 4 | import java.beans.PropertyEditorSupport; |
| 5 | import java.util.List; |
| 6 | |
| 6 | 7 | import org.springframework.beans.factory.annotation.Autowired; |
| 7 | 8 | import org.springframework.stereotype.Controller; |
| 8 | 9 | import org.springframework.ui.Model; |
| 10 | import org.springframework.web.bind.WebDataBinder; |
| 11 | import org.springframework.web.bind.annotation.InitBinder; |
| 12 | import org.springframework.web.bind.annotation.PathVariable; |
| 9 | 13 | import org.springframework.web.bind.annotation.RequestMapping; |
| 14 | import org.springframework.web.bind.annotation.RequestMethod; |
| 10 | 15 | import org.springframework.web.bind.annotation.RequestParam; |
| 11 | 16 | import org.springframework.web.bind.annotation.ResponseBody; |
| ... | ... | |
| 18 | 23 | @Autowired |
| 19 | 24 | private ContactService contactService; |
| 25 | @InitBinder |
| 26 | public void initBinder(WebDataBinder binder) { |
| 27 | binder.registerCustomEditor(Contact.class, new PropertyEditorSupport() { |
| 28 | @Override |
| 29 | public void setAsText(String text) throws IllegalArgumentException { |
| 30 | Long id = Long.parseLong(text); |
| 31 | setValue(contactService.findById(id)); |
| 32 | } |
| 33 | }); |
| 34 | } |
| 35 | |
| 20 | 36 | @RequestMapping("/") |
| 21 | 37 | String index(Model model) { |
| 22 | | model.addAttribute("msg", "A nice message"); |
| 23 | | |
| 24 | 38 | model.addAttribute("contacts", contactService.findAll()); |
| 25 | 39 | return "index"; |
| 26 | 40 | } |
| 27 | 41 | @RequestMapping("/contacts") |
| 28 | | @ResponseBody List<Contact> contacts(Model model) { |
| 42 | public @ResponseBody List<Contact> contacts(Model model) { |
| 29 | 43 | return contactService.findAll(); |
| 30 | 44 | } |
| 31 | | @RequestMapping("/add") |
| 32 | | String add(@RequestParam String name, Model model) { |
| 33 | | model.addAttribute("msg", "A nice message"); |
| 45 | @RequestMapping(value="/add", method = RequestMethod.GET) |
| 46 | public String addContact(Model model) { |
| 47 | model.addAttribute("contact", new Contact()); |
| 48 | return "contact"; |
| 49 | } |
| 50 | |
| 51 | @RequestMapping(value="/contact/{contact}", method = RequestMethod.GET) |
| 52 | public String showContact(@PathVariable("contact") Contact contact, Model model) { |
| 53 | model.addAttribute("contact", contact); |
| 54 | |
| 55 | return "contact"; |
| 56 | } |
| 57 | |
| 58 | @RequestMapping(value="/contact/{contact}", method = RequestMethod.POST) |
| 59 | public String updateContact(@PathVariable("contact") Contact originalContact, Contact contact, Model model) { |
| 60 | originalContact.setFirstName(contact.getFirstName()); |
| 61 | originalContact.setLastName(contact.getLastName()); |
| 62 | |
| 63 | model.addAttribute("contact", originalContact); |
| 64 | |
| 65 | contactService.update(originalContact); |
| 66 | |
| 67 | return "contact"; |
| 68 | } |
| 69 | |
| 70 | @RequestMapping(value="/add", method = RequestMethod.POST) |
| 71 | public String add(@RequestParam String firstName, @RequestParam String lastName, Model model) { |
| 34 | 72 | Contact contact = new Contact(); |
| 35 | | contact.setName(name); |
| 73 | contact.setFirstName(firstName); |
| 74 | contact.setLastName(lastName); |
| 36 | 75 | contactService.save(contact); |
| 76 | model.addAttribute("msg", "Contact added"); |
| 77 | |
| 37 | 78 | model.addAttribute("contacts", contactService.findAll()); |
| 38 | 79 | return "index"; |
| src/main/webapp/WEB-INF/view/contact.jsp |
| 1 | <?xml version='1.0' encoding='utf-8'?> |
| 2 | <jsp:root version="2.0" xmlns="http://www.w3c.org/1999/xhtml" |
| 3 | xmlns:jsp="http://java.sun.com/JSP/Page" |
| 4 | xmlns:c="http://java.sun.com/jsp/jstl/core" |
| 5 | xmlns:form="http://www.springframework.org/tags/form" |
| 6 | xmlns:spring="http://www.springframework.org/tags"> |
| 7 | |
| 8 | <jsp:directive.page contentType="text/html" pageEncoding="utf-8" /> |
| 9 | <jsp:output omit-xml-declaration="true" /> |
| 10 | <jsp:output doctype-root-element="html" doctype-system="about:legacy-compat" /> |
| 11 | |
| 12 | <html> |
| 13 | <head> |
| 14 | <meta charset="utf-8" /> |
| 15 | |
| 16 | <title>Spring MVC example</title> |
| 17 | </head> |
| 18 | |
| 19 | <body> |
| 20 | |
| 21 | <div> |
| 22 | <c:out value="${ msg }" /> |
| 23 | </div> |
| 24 | |
| 25 | <div> |
| 26 | <form:form commandName="contact" method="POST"> |
| 27 | First name: <form:input path="firstName" /><br/> |
| 28 | Last name: <form:input path="lastName" /><br/> |
| 29 | <input type="submit" value="Save Changes" /> |
| 30 | </form:form> |
| 31 | </div> |
| 32 | |
| 33 | </body> |
| 34 | </html> |
| 35 | </jsp:root> |