| src/main/java/info/ahlberg/spring/controller/ContactController.java |
| 1 | 1 | package info.ahlberg.spring.controller; |
| 2 | import java.util.List; |
| 3 | |
| 2 | 4 | import info.ahlberg.spring.domain.Contact; |
| 3 | 5 | import info.ahlberg.spring.service.ContactService; |
| ... | ... | |
| 8 | 10 | import org.springframework.ui.Model; |
| 9 | 11 | import org.springframework.web.bind.annotation.RequestMapping; |
| 10 | 12 | import org.springframework.web.bind.annotation.RequestParam; |
| 13 | import org.springframework.web.bind.annotation.ResponseBody; |
| 11 | 14 | @Controller |
| 12 | 15 | @RequestMapping("/") |
| 13 | 16 | public class ContactController { |
| 14 | | @Autowired |
| 15 | | private ContactService contactService; |
| 16 | | |
| 17 | | @RequestMapping("/") |
| 18 | | String index(Model model) { |
| 19 | | model.addAttribute("msg", "A nice message"); |
| 20 | | |
| 21 | | model.addAttribute("contacts", contactService.findAll()); |
| 17 | @Autowired |
| 18 | private ContactService contactService; |
| 19 | |
| 20 | @RequestMapping("/") |
| 21 | String index(Model model) { |
| 22 | model.addAttribute("msg", "A nice message"); |
| 23 | |
| 24 | model.addAttribute("contacts", contactService.findAll()); |
| 22 | | return "index"; |
| 23 | | } |
| 25 | return "index"; |
| 26 | } |
| 27 | @RequestMapping("/contacts") |
| 28 | @ResponseBody List<Contact> contacts(Model model) { |
| 29 | return contactService.findAll(); |
| 30 | } |
| 24 | | @RequestMapping("/add") |
| 25 | | String add(@RequestParam String name, Model model) { |
| 26 | | model.addAttribute("msg", "A nice message"); |
| 31 | @RequestMapping("/add") |
| 32 | String add(@RequestParam String name, Model model) { |
| 33 | model.addAttribute("msg", "A nice message"); |
| 27 | | Contact contact = new Contact(); |
| 28 | | contact.setName(name); |
| 29 | | contactService.save(contact); |
| 34 | Contact contact = new Contact(); |
| 35 | contact.setName(name); |
| 36 | contactService.save(contact); |
| 30 | | model.addAttribute("contacts", contactService.findAll()); |
| 37 | model.addAttribute("contacts", contactService.findAll()); |
| 31 | | return "index"; |
| 32 | | } |
| 38 | return "index"; |
| 39 | } |
| 33 | 40 | } |
| src/main/java/info/ahlberg/spring/domain/Contact.java |
| 10 | 10 | import javax.persistence.Version; |
| 11 | 11 | @Entity |
| 12 | | public class Contact implements Serializable { |
| 12 | public class Contact implements Serializable { |
| 13 | 13 | private static final long serialVersionUID = 1L; |
| 14 | 14 | @Id |
| ... | ... | |
| 23 | 23 | @Column(nullable = false) |
| 24 | 24 | private String name; |
| 25 | | public Long getId() { |
| 26 | | return id; |
| 27 | | } |
| 25 | public Long getId() { |
| 26 | return id; |
| 27 | } |
| 28 | | public void setId(Long id) { |
| 29 | | this.id = id; |
| 30 | | } |
| 28 | public void setId(Long id) { |
| 29 | this.id = id; |
| 30 | } |
| 31 | | public Long getVersion() { |
| 32 | | return version; |
| 33 | | } |
| 31 | public Long getVersion() { |
| 32 | return version; |
| 33 | } |
| 34 | | public void setVersion(Long version) { |
| 35 | | this.version = version; |
| 36 | | } |
| 34 | public void setVersion(Long version) { |
| 35 | this.version = version; |
| 36 | } |
| 37 | | public String getName() { |
| 38 | | return name; |
| 39 | | } |
| 37 | public String getName() { |
| 38 | return name; |
| 39 | } |
| 40 | | public void setName(String name) { |
| 41 | | this.name = name; |
| 42 | | } |
| 40 | public void setName(String name) { |
| 41 | this.name = name; |
| 42 | } |
| 43 | 43 | } |
| src/main/java/info/ahlberg/spring/service/ContactServiceImpl.java |
| 1 | 1 | package info.ahlberg.spring.service; |
| 2 | import java.util.List; |
| 3 | |
| 2 | 4 | import info.ahlberg.spring.dao.ContactDAO; |
| 3 | 5 | import info.ahlberg.spring.domain.Contact; |
| ... | ... | |
| 9 | 11 | @Service |
| 10 | 12 | public class ContactServiceImpl implements ContactService { |
| 11 | | @Autowired |
| 12 | | private ContactDAO contactDAO; |
| 13 | @Autowired |
| 14 | private ContactDAO contactDAO; |
| 13 | | @Override |
| 14 | | public Object findAll() { |
| 15 | | return contactDAO.loadAll(); |
| 16 | | } |
| 15 | @Override |
| 16 | public List<Contact> findAll() { |
| 17 | return contactDAO.loadAll(); |
| 18 | } |
| 17 | | @Override |
| 18 | | @Transactional |
| 19 | | public void save(Contact contact) { |
| 20 | | contactDAO.persist(contact); |
| 21 | | } |
| 19 | @Override |
| 20 | @Transactional |
| 21 | public void save(Contact contact) { |
| 22 | contactDAO.persist(contact); |
| 23 | } |
| 22 | 24 | } |