1.结构目录
2.dao.java
package struts.app;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Dao {
private static Map<Integer,Employee> emps=new HashMap<Integer,Employee>();
static {
emps.put(1001, new Employee(1001,"aa","AA","aa@qq.com"));
emps.put(1002, new Employee(1002,"bb","BB","bb@qq.com"));
emps.put(1005, new Employee(1005,"ff","FF","ff@qq.com"));
emps.put(1004, new Employee(1004,"dd","DD","dd@qq.com"));
emps.put(1003, new Employee(1003,"cc","CC","cc@qq.com"));
}
//查询
public List<Employee> getEmployee(){
return new ArrayList<>(emps.values());
}
//删除
public void delete(Integer empId) {
emps.remove(empId);
}
//添加
public void save(Employee emp) {
long time=System.currentTimeMillis();
emp.setEmployeeId((int)time);
emps.put(emp.getEmployeeId(),emp);
}
//获取
public Employee get(Integer empId) {
return emps.get(empId);
}
//修改
public void update(Employee emp) {
emps.put(emp.getEmployeeId(),emp);
}
}
3.Employee.java
package struts.app;
public class Employee {
private Integer employeeId;
private String firstName;
private String lastName;
private String email;
public Integer getEmployeeId() {
return employeeId;
}
public void setEmployeeId(Integer employeeId) {
this.employeeId = employeeId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Employee(Integer employeeId, String firstName, String lastName, String email) {
super();
this.employeeId = employeeId;
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
public Employee() {
super();
}
@Override
public String toString() {
return "Employee [employeeId=" + employeeId + ", firstName=" + firstName + ", lastName=" + lastName + ", email="
+ email + "]";
}
}
4.EmpoyeeAction.java
package struts.app;
import java.util.Map;
import org.apache.struts2.interceptor.RequestAware;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;
public class EmployeeAction implements RequestAware,ModelDriven<Employee>{
private Dao dao=new Dao();
private Employee employee;
private Integer employeeId;
public void setEmployeeId(Integer employeeId) {
this.employeeId = employeeId;
}
//查询
public String list() {
request.put("emps",dao.getEmployee());
return "list";
}
//添加
public String save() {
System.out.println("employee:"+ employee);
System.out.println("值栈站定对象:"+ ActionContext.getContext().getValueStack().peek());
//1.获取请求参数:通过定义对应属性的方式
//2.调用Dao的方法
dao.save(employee);
//3.通过redirectAction的方式响应结果给emp-list
return "success";
}
//删除
public String delete() {
dao.delete(employeeId);
//返回结果的类型应为:redirectAction.
//也可以是chain:实际上chain是没有必要的,因为不需要在下一个Action中保存当前的Action
//还有,若使用chain,则到达目标页面后,地址栏显示的依然是删除的那个链接,刷新时会有重复提交。
return "success";
}
//修改
public String edit(){
//1.获取传入的employeeId:employee.getEmployeeId()
//2.根据employeesId 获取employee对象
//Employee emp2 = dao.get(employee.getEmployeeId());
//3.把栈顶的对象的属性装配好:此时的栈顶对象是employee
//目前的employee 对象只有 employeeId 属性,其他属性为 null
/*
employee.setFirstName(emp2.getFirstName());
employee.setLastName(emp2.getLastName());
employee.setEmail(emp2.getEmail());
*/
return "edit";
}
//更新
public String update() {
dao.update(employee);
return "success";
}
private Map<String,Object> request;
@Override
public void setRequest(Map<String, Object> arg0) {
this.request=arg0;
}
@Override
public Employee getModel() {
//判断Create 还是 edit
//若为Create,则:employeeBean = new EmployeeBean();
//若为edit ,则从数据库中获取 employeeBean = dao.get(employeeBean.getEmployeeId());
//判断标准为:是否有employeeId。若有则视为 edit,若没有则视为 Create
//若通过employeeId 来判断,则需要在modelDriven 拦截器之前先执行一个params拦截器
//可以通过使用paramsPrepareParams 拦截器实现
//需要在 struts.xml 文件中配置 paramsPrepareParams 为默认的拦截器栈
if(employeeId == null){
employee = new Employee();
} else {
employee = dao.get(employeeId);
}
return employee;
}
}
5.struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="struts" namespace="" extends="struts-default">
<default-interceptor-ref name="paramsPrepareParamsStack"></default-interceptor-ref>
<action name="emp-*" class="struts.app.EmployeeAction" method="{1}">
<result name="{1}">/emp-{1}.jsp</result>
<result name="success" type="redirectAction">/emp-list</result>
</action>
</package>
</struts>
6.index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="emp-list.action">直接跳转页面</a>
</body>
</html>
7.emp-list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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>Insert title here</title>
</head>
<body>
<s:debug></s:debug>
<form action="emp-save">
<s:textfield name="firstName" label="firstName" ></s:textfield>
<s:textfield name="lastName" label="lastName"></s:textfield>
<s:textfield name="email" label="email" ></s:textfield>
<s:submit></s:submit>
</form>
<br>
<hr>
<br>
<table cellpadding="10" cellspacing="0" border="1">
<thead>
<tr>
<td>ID</td>
<td>FirstName</td>
<td>LastName</td>
<td>Email</td>
<td>Edit</td>
<td>Delete</td>
</tr>
</thead>
<body>
<s:iterator value="#request.emps">
<tr>
<td>${employeeId}</td>
<td>${firstName}</td>
<td>${lastName}</td>
<td>${email}</td>
<td><a href="emp-edit?employeeId=${employeeId}">Edit</a></td>
<td><a href="emp-delete?employeeId=${employeeId}">Delete</a></td>
</tr>
</s:iterator>
</body>
</table>
</body>
</html>
8.edit-list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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>Insert title here</title>
</head>
<body>
<s:debug></s:debug>
<form action="emp-update">
<s:hidden name="employeeId"></s:hidden>
<s:textfield name="firstName" label="firstName" ></s:textfield>
<s:textfield name="lastName" label="lastName"></s:textfield>
<s:textfield name="email" label="email" ></s:textfield>
<s:submit></s:submit>
</form>
</body>
</html>
页面结果: