SSH_框架整合6--修改Edit员工信息
1 加上修改Edit键
(1)emp-list.jsp
<td> <a href="emp-input?id=${id }">Edit</a> </td>
2 表单回显---
方法一:通过隐藏域的方法
(1)EmployeeDao.java
1 //5 Edit 2 public Employee get(Integer id){ 3 return (Employee) getSession().get(Employee.class, id); 4 }
(2)EmployeeService.java
1 //5 Edit 2 public Employee get(Integer id){ 3 return employeeDao.get(id); 4 }
(3)EmployeeAction.java
1 public String save(){ 2 //第一次添加 就加上系统时间,否则直接保存不修改时间 3 if(id == null){ 4 model.setCreateTime(new Date()); 5 } 6 employeeService.saveorUpdate(model); 7 return SUCCESS; 8 } 9 10 public void prepareSave(){ 11 model = employeeService.get(id); 12 } 13 14 //**** 6-1 拦截器的方法 15 public void prepareInput(){ 16 //说明在修改,这时从数据库中获取状态 17 if(id != null){ 18 model = employeeService.get(id); 19 } 20 }
(4) emp-input.jsp
1 <s:form action="emp-save" method="post"> 2 <s:if test="id != null"> 3 <s:textfield name="lastName" label="LastName" disabled="true"></s:textfield> 4 <s:hidden name="id"></s:hidden> 5 6 <%--通过隐藏域的方法--%> 7 <s:hidden name="lastName"></s:hidden> 8 <s:hidden name="createTime"></s:hidden> 9 </s:if> 10 11 <s:else> 12 <s:textfield name="lastName" label="LastName"></s:textfield> 13 </s:else> 14 <s:textfield name="email" label="Email"></s:textfield> 15 <s:textfield name="birth" label="Birth"></s:textfield> 16 17 <s:select list="#request.departments" 18 listKey="id" listValue="departmentName" name="department.id" 19 label="Department"></s:select> 20 <s:submit></s:submit> 21 </s:form>
方法二:通过值栈获取的方法:
(1)(2)同
(3)EmployeeAction.java
1 //1:获取所有的部门的信息 2 public String input(){ 3 request.put("departmentsInfo", departmentService.getAll()); 4 return INPUT; 5 } 6 public void prepareInput(){ 7 //说明在修改信息 8 if(id != null){ 9 model = employeeService.get(id); 10 } 11 } 12 /** 13 * 5 修改 14 */ 15 //2: 使用拦截器存储信息 16 public String save(){ 17 //第一次添加。否则就是修改,不执行这一步 18 if(id == null){ 19 model.setCreateTime(new Date()); 20 } 21 employeeService.saveOrUpdate(model); 22 return SUCCESS; 23 } 24 25 public void prepareSave(){ 26 //第一次添加,重新获取 27 if(id == null){ 28 model = new Employee(); 29 } 30 //修改,从数据库获取 31 else{ 32 model = employeeService.get(id); 33 } 34 }
(4) emp-input.jsp
1 <s:form action="emp-save" method="post"> 2 <s:if test="id != null"> 3 <s:textfield name="lastName" label="LastName" disabled="true"></s:textfield> 4 <s:hidden name="id"></s:hidden> 5 6 <%--通过隐藏域的方法 7 <s:hidden name="lastName"></s:hidden> 8 <s:hidden name="createTime"></s:hidden> --%> 9 </s:if> 10 11 <s:else> 12 <s:textfield name="lastName" label="LastName"></s:textfield> 13 </s:else> 14 <s:textfield name="email" label="Email"></s:textfield> 15 <s:textfield name="birth" label="Birth"></s:textfield> 16 17 <s:select list="#request.departments" 18 listKey="id" listValue="departmentName" name="department.id" 19 label="Department"></s:select> 20 <s:submit></s:submit> 21 </s:form>