|
1 | 1 | package com.baeldung.spring.controller; |
2 | 2 |
|
3 | | -import javax.validation.Valid; |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.Map; |
4 | 5 |
|
5 | | -import com.baeldung.spring.form.Employee; |
6 | 6 | import org.springframework.stereotype.Controller; |
7 | 7 | import org.springframework.ui.ModelMap; |
8 | 8 | import org.springframework.validation.BindingResult; |
9 | 9 | import org.springframework.web.bind.annotation.ModelAttribute; |
| 10 | +import org.springframework.web.bind.annotation.PathVariable; |
10 | 11 | import org.springframework.web.bind.annotation.RequestMapping; |
11 | 12 | import org.springframework.web.bind.annotation.RequestMethod; |
| 13 | +import org.springframework.web.bind.annotation.ResponseBody; |
12 | 14 | import org.springframework.web.servlet.ModelAndView; |
13 | 15 |
|
| 16 | +import com.baeldung.spring.form.Employee; |
| 17 | + |
14 | 18 | @Controller |
15 | 19 | public class EmployeeController { |
16 | 20 |
|
17 | | - @RequestMapping(value = "/employee", method = RequestMethod.GET) |
18 | | - public ModelAndView showForm() { |
19 | | - return new ModelAndView("employeeHome", "employee", new Employee()); |
20 | | - } |
21 | | - |
22 | | - @RequestMapping(value = "/addEmployee", method = RequestMethod.POST) |
23 | | - public String submit(@Valid @ModelAttribute("employee") final Employee employee, final BindingResult result, final ModelMap model) { |
24 | | - if (result.hasErrors()) { |
25 | | - return "error"; |
26 | | - } |
27 | | - |
28 | | - model.addAttribute("name", employee.getName()); |
29 | | - model.addAttribute("contactNumber", employee.getContactNumber()); |
30 | | - model.addAttribute("id", employee.getId()); |
31 | | - return "employeeView"; |
32 | | - } |
| 21 | + Map<Long, Employee> employeeMap = new HashMap<>(); |
| 22 | + |
| 23 | + @RequestMapping(value = "/employee", method = RequestMethod.GET) |
| 24 | + public ModelAndView showForm() { |
| 25 | + return new ModelAndView("employeeHome", "employee", new Employee()); |
| 26 | + } |
| 27 | + |
| 28 | + @RequestMapping(value = "/employee/{Id}", produces = { "application/json", "application/xml" }, method = RequestMethod.GET) |
| 29 | + public @ResponseBody Employee getEmployeeById(@PathVariable final long Id) { |
| 30 | + return employeeMap.get(Id); |
| 31 | + } |
| 32 | + |
| 33 | + @RequestMapping(value = "/employee/{Id}", method = RequestMethod.GET) |
| 34 | + public String getEmployeeByIdHtmlView(@PathVariable final long Id, final ModelMap model) { |
| 35 | + model.addAttribute("name", employeeMap.get(Id).getName()); |
| 36 | + model.addAttribute("contactNumber", employeeMap.get(Id).getContactNumber()); |
| 37 | + model.addAttribute("id", employeeMap.get(Id).getId()); |
| 38 | + |
| 39 | + return "employeeView"; |
| 40 | + } |
| 41 | + |
| 42 | + @RequestMapping(value = "/addEmployee", method = RequestMethod.POST) |
| 43 | + public String submit(@ModelAttribute("employee") final Employee employee, final BindingResult result, final ModelMap model) { |
| 44 | + if (result.hasErrors()) { |
| 45 | + return "error"; |
| 46 | + } |
| 47 | + model.addAttribute("name", employee.getName()); |
| 48 | + model.addAttribute("contactNumber", employee.getContactNumber()); |
| 49 | + model.addAttribute("id", employee.getId()); |
| 50 | + |
| 51 | + employeeMap.put(employee.getId(), employee); |
| 52 | + |
| 53 | + return "employeeView"; |
| 54 | + } |
33 | 55 |
|
34 | 56 | } |
0 commit comments