victory的博客

长安一片月,万户捣衣声

0%

SSM | 修改

修改员工信息

1.list.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>展示员工信息</title>
<link rel="stylesheet" href="${pageContext.servletContext.contextPath}/css/index_work.css"/>
</head>
<body>
    <table>
        <tr>
            <th>EID</th>
            <th>ENAME</th>
            <th>AGE</th>
            <th>SEX</th>
            <th>DEPARTMENTNAME</th>
            <th>OPTIONS</th>
        </tr>
        <c:forEach items="${empList}" var="emp">
            <tr>
                <td>${emp.eid}</td>
                <td>${emp.ename}</td>
                <td>${emp.age}</td>
                <td>${emp.sex == 0 ? '女':'男'}</td>
                <td>${emp.dept.dname}</td>
                <td>
                    <a href="emp">删除</a>
                    <a href="${pageContext.servletContext.contextPath}/emp/${emp.eid}">修改</a>
                </td>
            </tr>
        </c:forEach>
        <tr>
            <td colspan="10">
                ${page}
            </td>
        </tr>
    </table>
</body>
</html>

2.update.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>修改员工信息</title>
<link rel="stylesheet" href="${pageContext.servletContext.contextPath}/css/index_work.css"/>
</head>
<body>
    <form:form action="${pageContext.servletContext.contextPath}/emp" method="post" modelAttribute="emp">
        <input type="hidden" name="_method" value="PUT"/>
        <form:hidden path="eid"/>
        <table>
            <tr>
                <th colspan="2">UPDATE EMP INFO</th>
            </tr>
            <tr>
                <td>ENAME</td>
                <td>
                    <form:input path="ename"/>
                </td>
            </tr>
            <tr>
                <td>AGE</td>
                <td>
                    <form:input path="age"/>
                </td>
            </tr>
            <tr>
                <td>sex</td>
                <td>
                    <form:radiobuttons path="sex" items="${sex}"/>
                </td>
            </tr>
            <tr>
                <td>DEPARTMENT</td>
                <td>
                    <form:select path="dept.did" items="${deptList}" itemLabel="dname" itemValue="did"></form:select>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <input type="submit" value="修改" />
                </td>
            </tr>
        </table>
    </form:form>
</body>
</html>

3.EmpController.java

@Controller
public class EmpController {

    @Autowired
    private EmpService service;
    
    @RequestMapping(value="/emp/{eid}", method=RequestMethod.GET)
    public String toUpdate(@PathVariable("eid")String eid, Map<String, Object> map){
        //要修改的员工信息
        Emp emp = service.getEmpByEid(eid);
        
        //所有的部门信息
        List<Dept> deptList = service.getAllDept();
        
        //获取存储性别的map集合
        Map<String, String> sex = new HashMap<>();
        sex.put("0", "女");
        sex.put("1", "男");
        
        map.put("emp", emp);
        map.put("deptList", deptList);
        map.put("sex", sex);
        
        return "update";
    }
    
    @RequestMapping(value="/emp", method=RequestMethod.PUT)
    public String updateEmp(Emp emp){
        service.updateEmp(emp);
        return "redirect:/emps/1";
    }
}

4.EmpService.java

public interface EmpService {
    ......
    
    List<Dept> getAllDept();
}

5.EmpServiceImpl.java

@Service
public class EmpServiceImpl implements EmpService{
    @Autowired
    private EmpMapper empMapper;
    
    @Autowired
    private DeptMapper deptMapper;
    
    ......
    
    @Override
    public List<Dept> getAllDept() {
        // TODO Auto-generated method stub
        return deptMapper.getAllDept();
    }
}