本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用
内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系。
本人互联网技术爱好者,互联网技术发烧友
微博:伊直都在0221
QQ:951226918
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1.代码结构
2.代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.jason.strut2.curd; 2 3 public class EmployeeBean { 4 5 private Integer employeeId; 6 private String firstNmame; 7 private String lastName; 8 private String email; 9 10 public Integer getEmployeeId() { 11 return employeeId; 12 } 13 14 public void setEmployeeId(Integer employeeId) { 15 this.employeeId = employeeId; 16 } 17 18 public String getFirstNmame() { 19 return firstNmame; 20 } 21 22 public void setFirstNmame(String firstNmame) { 23 this.firstNmame = firstNmame; 24 } 25 26 public String getLastName() { 27 return lastName; 28 } 29 30 public void setLastName(String lastName) { 31 this.lastName = lastName; 32 } 33 34 public String getEmail() { 35 return email; 36 } 37 38 public void setEmail(String email) { 39 this.email = email; 40 } 41 42 public EmployeeBean(Integer employeeId, String firstNmame, String lastName, 43 String email) { 44 super(); 45 this.employeeId = employeeId; 46 this.firstNmame = firstNmame; 47 this.lastName = lastName; 48 this.email = email; 49 } 50 51 public EmployeeBean() { 52 super(); 53 } 54 55 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.jason.strut2.curd; 2 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.List; 6 import java.util.Map; 7 8 public class Dao { 9 10 private static Map<Integer, EmployeeBean> employees = new HashMap<Integer, EmployeeBean>(); 11 12 static{ 13 14 employees.put(1001, new EmployeeBean(1001,"AA","aa","AA@163.com")); 15 employees.put(1002, new EmployeeBean(1002,"BB","bb","BB@163.com")); 16 employees.put(1003, new EmployeeBean(1003,"CC","cc","CC@163.com")); 17 employees.put(1004, new EmployeeBean(1004,"DD","dd","DD@163.com")); 18 employees.put(1005, new EmployeeBean(1005,"EE","ee","EE@163.com")); 19 20 } 21 22 23 //查看 24 public List<EmployeeBean> getEmployees(){ 25 return new ArrayList<>(employees.values()); 26 } 27 28 //删除 29 public void delete(Integer id){ 30 employees.remove(id); 31 } 32 33 //保存 34 public void save(EmployeeBean employeeBean){ 35 long time = System.currentTimeMillis(); 36 employeeBean.setEmployeeId((int) time); 37 employees.put(employeeBean.getEmployeeId(), employeeBean); 38 39 } 40 41 //获取 42 public EmployeeBean get(Integer employeeId){ 43 44 return employees.get(employeeId); 45 } 46 47 //更新 48 public void update(EmployeeBean employeeBean){ 49 employees.put(employeeBean.getEmployeeId(), employeeBean); 50 } 51 52 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.jason.strut2.curd; 2 3 import java.util.Map; 4 5 import org.apache.struts2.interceptor.RequestAware; 6 7 public class EmployeeAction implements RequestAware { 8 9 private Dao dao = new Dao(); 10 private Map<String, Object> requestMap; 11 12 // 需要在当前的 EmployeeAction 中定义employeeId 属性,以接收页面请求参数:删除操作 13 private Integer employeeId; 14 15 public String list() { 16 17 requestMap.put("emps", dao.getEmployees()); 18 return "list"; 19 } 20 21 public String delete() { 22 23 dao.delete(employeeId); 24 // 返回结果的类型应为 :redirectAction,也可以是chain:实际chain 是没有必要 25 // 还有,若使用chain 则到达目标页面后,地址栏显示的依然是 删除 的那个连接,刷新时 会有重复提交 26 return "delete"; 27 } 28 29 @Override 30 public void setRequest(Map<String, Object> requestMap) { 31 this.requestMap = requestMap; 32 } 33 34 public Integer getEmployeeId() { 35 return employeeId; 36 } 37 38 public void setEmployeeId(Integer employeeId) { 39 this.employeeId = employeeId; 40 } 41 42 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 11 <a href="emp-list">List All Employees</a> 12 13 </body> 14 </html>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 6 <struts> 7 8 <constant name="struts.action.extension" value="action,do,"></constant> 9 <package name="default" namespace="/" extends="struts-default"> 10 11 <action name="emp-*" class="com.jason.strut2.curd.EmployeeAction" method="{1}"> 12 <result name="{1}">/emp-{1}.jsp</result> 13 <result name="delete" type="redirectAction">emp-list</result> 14 </action> 15 16 17 </package> 18 </struts>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 11 <a href="emp-list">List All Employees</a> 12 13 </body> 14 </html>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 4 <%@ taglib prefix="s" uri="/struts-tags" %> 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 6 <html> 7 <head> 8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 9 <title>Insert title here</title> 10 </head> 11 <body> 12 13 <table cellpadding="10" cellspacing="0" border="1"> 14 <thead> 15 <tr> 16 <td>ID</td> 17 <td>FirstName</td> 18 <td>LastName</td> 19 <td>Email</td> 20 <td>Edit</td> 21 <td>Delete</td> 22 </tr> 23 </thead> 24 <tbody> 25 <s:iterator value="#request.emps"> 26 <tr> 27 <td>${employeeId}</td> 28 <td>${firstName}</td> 29 <td>${lastName}</td> 30 <td>${email}</td> 31 <td><a href="">Eidt</a></td> 32 <td><a href="emp-delete?employeeId=${employeeId }">Delete</a></td> 33 </tr> 34 </s:iterator> 35 </tbody> 36 37 </table> 38 39 40 </body> 41 </html>
3.Struts2 运行流程图-1
4.Params 拦截器:
Parameters 拦截器将把表单字段映射到 ValueStack 栈的栈顶对象的各个属性中. 如果某个字段在模型里没有匹配的属性, Param 拦截器将尝试 ValueStack 栈中的下一个对象
5.ModelDriven拦截器
1)当用户触发 add 请求时, ModelDriven 拦截器将调用 EmployeeAction 对象的 getModel() 方法, 并把返回的模型(Employee实例)压入到 ValueStack 栈.
2)接下来 Parameters 拦截器将把表单字段映射到 ValueStack 栈的栈顶对象的各个属性中. 因为此时 ValueStack 栈的栈顶元素是刚被压入的模型(Employee)对象, 所以该模型将被填充. 如果某个字段在模型里没有匹配的属性, Param 拦截器将尝试 ValueStack 栈中的下一个对象
Action 实现ModelDriven 接口后的运行流程
1 @Override 2 public String intercept(ActionInvocation invocation) throws Exception { 3 4 //获取Action 对象:EmployeeAction对象,此时该Action 已经实现了ModelDriven 接口 5 //public class EmployeeAction implements RequestAware ,ModelDriven<EmployeeBean> 6 Object action = invocation.getAction(); 7 //判断:action是否是ModelDriven 的实例 8 if (action instanceof ModelDriven) { 9 //强制转换 ModelDriven 类型 10 ModelDriven modelDriven = (ModelDriven) action; 11 //获取值栈 12 ValueStack stack = invocation.getStack(); 13 //调用 ModelDriven 接口的getModel()方法 14 //即 调用 EmployeeAction 的 getModel()方法 15 /* 16 * @Override 17 public EmployeeBean getModel() { 18 employeeBean = new EmployeeBean(); 19 return employeeBean; 20 } 21 22 */ 23 24 Object model = modelDriven.getModel(); 25 if (model != null) { 26 //把getModel 的返回值压入到值栈的栈顶:实际压入的是employeeAction 的成员变量,employeeBean 27 stack.push(model); 28 } 29 if (refreshModelBeforeResult) { 30 invocation.addPreResultListener(new RefreshModelBeforeResult(modelDriven, model)); 31 } 32 } 33 return invocation.invoke(); 34 } 35 36 37 //执行:PramaterInterception 的intercept() 方法:把请求的参数的值赋给栈顶对象,对应的属性, 38 //若栈顶对象没有对应的属性,则查询值栈中的下一个对象,直到找到此属性,赋值。
注意:getModel()方法不能如下实现
因为 可以将此对象压入值栈的栈顶;但是当前的Action 成员变量是 null;
public EmployeeBean getModel() {
return new Employee();
}
实现modelDriven 接口
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.jason.strut2.curd; 2 3 import java.util.Map; 4 5 import org.apache.struts2.interceptor.RequestAware; 6 7 import com.opensymphony.xwork2.ActionInvocation; 8 import com.opensymphony.xwork2.ModelDriven; 9 import com.opensymphony.xwork2.util.ValueStack; 10 11 public class EmployeeAction implements RequestAware ,ModelDriven<EmployeeBean>{ 12 13 private Dao dao = new Dao(); 14 private Map<String, Object> requestMap; 15 private EmployeeBean employeeBean; 16 17 // 需要在当前的 EmployeeAction 中定义employeeId 属性,以接收页面请求参数:删除操作 18 19 20 public String list() { 21 22 requestMap.put("emps", dao.getEmployees()); 23 return "list"; 24 } 25 26 //删除 27 public String delete() { 28 29 dao.delete(employeeBean.getEmployeeId()); 30 // 返回结果的类型应为 :redirectAction,也可以是chain:实际chain 是没有必要 31 // 还有,若使用chain 则到达目标页面后,地址栏显示的依然是 删除 的那个连接,刷新时 会有重复提交 32 return "success"; 33 } 34 35 36 public String save(){ 37 38 //1.获取请求参数:通过定义属性的方式 39 //2.调用DAO的 svae 方法 40 41 dao.save(employeeBean); 42 //3.通过redirectAction 的方式响应结果给 emp-list 43 return "success"; 44 } 45 46 @Override 47 public void setRequest(Map<String, Object> requestMap) { 48 this.requestMap = requestMap; 49 } 50 51 52 53 @Override 54 public EmployeeBean getModel() { 55 employeeBean = new EmployeeBean(); 56 return employeeBean; 57 } 58 59 }
图示说明: