Skip to content

Commit b9ddf89

Browse files
committed
Content Negotiation Strategy
1 parent 800285f commit b9ddf89

1 file changed

Lines changed: 40 additions & 18 deletions

File tree

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,56 @@
11
package com.baeldung.spring.controller;
22

3-
import javax.validation.Valid;
3+
import java.util.HashMap;
4+
import java.util.Map;
45

5-
import com.baeldung.spring.form.Employee;
66
import org.springframework.stereotype.Controller;
77
import org.springframework.ui.ModelMap;
88
import org.springframework.validation.BindingResult;
99
import org.springframework.web.bind.annotation.ModelAttribute;
10+
import org.springframework.web.bind.annotation.PathVariable;
1011
import org.springframework.web.bind.annotation.RequestMapping;
1112
import org.springframework.web.bind.annotation.RequestMethod;
13+
import org.springframework.web.bind.annotation.ResponseBody;
1214
import org.springframework.web.servlet.ModelAndView;
1315

16+
import com.baeldung.spring.form.Employee;
17+
1418
@Controller
1519
public class EmployeeController {
1620

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+
}
3355

3456
}

0 commit comments

Comments
 (0)