|
| 1 | +package com.demo.controllers; |
| 2 | + |
| 3 | +import javax.validation.Valid; |
| 4 | + |
| 5 | +import org.springframework.stereotype.Controller; |
| 6 | +import org.springframework.ui.ModelMap; |
| 7 | +import org.springframework.validation.BindingResult; |
| 8 | +import org.springframework.web.bind.annotation.ModelAttribute; |
| 9 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 10 | +import org.springframework.web.bind.annotation.RequestMethod; |
| 11 | +import org.springframework.web.servlet.ModelAndView; |
| 12 | + |
| 13 | +import com.demo.form.Employee; |
| 14 | + |
| 15 | +@Controller |
| 16 | +public class EmployeeController { |
| 17 | + |
| 18 | + @RequestMapping(value = "/employee", method = RequestMethod.GET) |
| 19 | + public ModelAndView showForm() { |
| 20 | + return new ModelAndView("employeeHome", "employee", new Employee()); |
| 21 | + } |
| 22 | + |
| 23 | + @RequestMapping(value = "/addEmployee", method = RequestMethod.POST) |
| 24 | + public String submit(@Valid @ModelAttribute("employee")Employee employee, BindingResult result, |
| 25 | + ModelMap model) { |
| 26 | + if (result.hasErrors()) { |
| 27 | + return "error"; |
| 28 | + } |
| 29 | + model.addAttribute("name", employee.getName()); |
| 30 | + model.addAttribute("contactNumber", employee.getContactNumber()); |
| 31 | + model.addAttribute("id", employee.getId()); |
| 32 | + return "employeeAdded"; |
| 33 | + } |
| 34 | +} |
0 commit comments