| 1 | package info.ahlberg.spring; |
| 2 | |
| 3 | import org.springframework.beans.factory.annotation.Autowired; |
| 4 | import org.springframework.stereotype.Controller; |
| 5 | import org.springframework.ui.Model; |
| 6 | import org.springframework.web.bind.annotation.RequestMapping; |
| 7 | import org.springframework.web.bind.annotation.RequestParam; |
| 8 | |
| 9 | @Controller |
| 10 | @RequestMapping("/") |
| 11 | public class ContactController { |
| 12 | @Autowired |
| 13 | private ContactService contactService; |
| 14 | |
| 15 | @RequestMapping("/") |
| 16 | String index(Model model) { |
| 17 | model.addAttribute("msg", "A nice message"); |
| 18 | |
| 19 | model.addAttribute("contacts", contactService.findAll()); |
| 20 | |
| 21 | return "index"; |
| 22 | } |
| 23 | |
| 24 | |
| 25 | @RequestMapping("/add") |
| 26 | String add(@RequestParam String name, Model model) { |
| 27 | model.addAttribute("msg", "A nice message"); |
| 28 | |
| 29 | Contact contact = new Contact(); |
| 30 | contact.setName(name); |
| 31 | contactService.save(contact); |
| 32 | |
| 33 | model.addAttribute("contacts", contactService.findAll()); |
| 34 | |
| 35 | return "index"; |
| 36 | } |
| 37 | } |
| 38 | |