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