SpringBoot 之 控制器层

@Controller public class EmployeeController { @Autowired EmployeeDao employeeDao; @Autowired DepartmentDao departmentDao; @GetMapping("/employees") publi...
@Controller
public class EmployeeController {

    @Autowired
    EmployeeDao employeeDao;

    @Autowired
    DepartmentDao departmentDao;

    @GetMapping("/employees")
    public String index(Model model) {
        Collection<Employee> employees = employeeDao.index();
        model.addAttribute("employees", employees);
        return "employees/index";
    }

    @PostMapping("/employees")
    public String store(Employee employee) {
        employeeDao.store(employee);
        return "redirect:/employees";
    }

    @GetMapping("/employees/{id}")
    public String show(@PathVariable("id") Integer id, Model model) {
        Employee employee = employeeDao.show(id);
        model.addAttribute("employee", employee);
        Collection<Department> departments = departmentDao.index();
        model.addAttribute("departments", departments);
        return "employees/show";
    }

    @PutMapping("/employees/{id}")
    public String update(@PathVariable("id") Integer id, Employee employee) {
        employee.setId(id);
        employeeDao.update(employee);
        return "redirect:/employees";
    }

    @DeleteMapping("/employees/{id}")
    public String destroy(@PathVariable("id") Integer id) {
        employeeDao.destroy(id);
        return "redirect:/employees";
    }
}
  • 发表于 2020-05-05 20:38
  • 阅读 ( 165 )
  • 分类:网络文章

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除