SpringMVC----RESTFUL_CRUD_修改操作

2021-05-20 15:28

阅读:458

YPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

标签:lan   why   body   str   信息   html4   文件中   orm   BMI   

1.注意:


 

2.代码:

list.jsp


    pageEncoding="UTF-8"%>Insert title here
if test="${empty requestScope.employees }"> 没有任何员工信息. if> if test="${!empty requestScope.employees }">
ID LastName Email Gender Department Edit Delete
${emp.id } ${emp.lastName } ${emp.email } ${emp.gender == 0 ? ‘Female‘ : ‘Male‘ } ${emp.department.departmentName } Edit class="delete" href="http://www.soscw.com/emp/${emp.id}">Delete
if>

Add New Employee input.jsp import="java.util.HashMap"%> import="java.util.Map"%> pageEncoding="UTF-8"%> Insert title here
method="POST" modelAttribute="employee"> LastName:
email:
Map genders = new HashMap(); genders.put("1", "male"); genders.put("0", "Female"); request.setAttribute("genders", genders); %> gender:
Department:
java代码: package com.yikuan.springmvc.crud.handlers; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import com.yikuan.springmvc.crud.dao.DepartmentDao; import com.yikuan.springmvc.crud.dao.EmployeeDao; import com.yikuan.springmvc.crud.entities.Employee; @Controller public class EmployeeHandler { @Autowired private EmployeeDao employeeDao; @Autowired private DepartmentDao departmentDao; @ModelAttribute public void getEmployee(@RequestParam(value="id",required=false)Integer id, Map map){ if(id != null){ map.put("employee", employeeDao.get(id)); } } @RequestMapping(value="/emp",method=RequestMethod.PUT) public String update(Employee employee){ employeeDao.save(employee); //此时修改完后,lastName为null@ModelAttribute; return "redirect:/emps"; } @RequestMapping(value="/emp/{id}",method=RequestMethod.GET) public String input(@PathVariable("id") Integer id,Map map){ map.put("employee", employeeDao.get(id)); /*employee必须和input.jsp中表单页面的modelAttribute="employee"值一样*/ map.put("departments", departmentDao.getDepartments()); return "input"; } @RequestMapping(value="/emp/{id}",method=RequestMethod.DELETE) public String delete(@PathVariable("id") Integer id){ employeeDao.delete(id); return "redirect:/emps"; } @RequestMapping(value="/emp",method=RequestMethod.POST) public String save(Employee employee){ employeeDao.save(employee); return "redirect:/emps"; } @RequestMapping(value="emp",method=RequestMethod.GET) public String input(Map map){ map.put("departments", departmentDao.getDepartments()); map.put("employee", new Employee()); return "input"; } @RequestMapping("/emps") public String list(Map map){ map.put("employees", employeeDao.getAll()); return "list"; } }

 

SpringMVC----RESTFUL_CRUD_修改操作

标签:lan   why   body   str   信息   html4   文件中   orm   BMI   

原文地址:https://www.cnblogs.com/yikuan-919/p/9741153.html


评论


亲,登录后才可以留言!