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