[原创]java WEB学习笔记65:Struts2 学习之路--Struts的CRUD操作( 查看 / 删除/ 添加) ModelDriven拦截器 paramter 拦截器

本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用

内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系。

本人互联网技术爱好者,互联网技术发烧友

微博:伊直都在0221

QQ:951226918

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

1.代码结构

[原创]java WEB学习笔记65:Struts2 学习之路--Struts的CRUD操作( 查看 / 删除/ 添加)  ModelDriven拦截器  paramter 拦截器

2.代码

 package com.jason.strut2.curd;

 public class EmployeeBean {

     private Integer employeeId;
private String firstNmame;
private String lastName;
private String email; public Integer getEmployeeId() {
return employeeId;
} public void setEmployeeId(Integer employeeId) {
this.employeeId = employeeId;
} public String getFirstNmame() {
return firstNmame;
} public void setFirstNmame(String firstNmame) {
this.firstNmame = firstNmame;
} 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 EmployeeBean(Integer employeeId, String firstNmame, String lastName,
String email) {
super();
this.employeeId = employeeId;
this.firstNmame = firstNmame;
this.lastName = lastName;
this.email = email;
} public EmployeeBean() {
super();
} }

EmployeeBean

 package com.jason.strut2.curd;

 import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; public class Dao { private static Map<Integer, EmployeeBean> employees = new HashMap<Integer, EmployeeBean>(); static{ employees.put(1001, new EmployeeBean(1001,"AA","aa","AA@163.com"));
employees.put(1002, new EmployeeBean(1002,"BB","bb","BB@163.com"));
employees.put(1003, new EmployeeBean(1003,"CC","cc","CC@163.com"));
employees.put(1004, new EmployeeBean(1004,"DD","dd","DD@163.com"));
employees.put(1005, new EmployeeBean(1005,"EE","ee","EE@163.com")); } //查看
public List<EmployeeBean> getEmployees(){
return new ArrayList<>(employees.values());
} //删除
public void delete(Integer id){
employees.remove(id);
} //保存
public void save(EmployeeBean employeeBean){
long time = System.currentTimeMillis();
employeeBean.setEmployeeId((int) time);
employees.put(employeeBean.getEmployeeId(), employeeBean); } //获取
public EmployeeBean get(Integer employeeId){ return employees.get(employeeId);
} //更新
public void update(EmployeeBean employeeBean){
employees.put(employeeBean.getEmployeeId(), employeeBean);
} }

Dao

 package com.jason.strut2.curd;

 import java.util.Map;

 import org.apache.struts2.interceptor.RequestAware;

 public class EmployeeAction implements RequestAware {

     private Dao dao = new Dao();
private Map<String, Object> requestMap; // 需要在当前的 EmployeeAction 中定义employeeId 属性,以接收页面请求参数:删除操作
private Integer employeeId; public String list() { requestMap.put("emps", dao.getEmployees());
return "list";
} public String delete() { dao.delete(employeeId);
// 返回结果的类型应为 :redirectAction,也可以是chain:实际chain 是没有必要
// 还有,若使用chain 则到达目标页面后,地址栏显示的依然是 删除 的那个连接,刷新时 会有重复提交
return "delete";
} @Override
public void setRequest(Map<String, Object> requestMap) {
this.requestMap = requestMap;
} public Integer getEmployeeId() {
return employeeId;
} public void setEmployeeId(Integer employeeId) {
this.employeeId = employeeId;
} }

EmployeeAction

 <%@ 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=UTF-8">
<title>Insert title here</title>
</head>
<body> <a href="emp-list">List All Employees</a> </body>
</html>

web.xml

  

 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.action.extension" value="action,do,"></constant>
<package name="default" namespace="/" extends="struts-default"> <action name="emp-*" class="com.jason.strut2.curd.EmployeeAction" method="{1}">
<result name="{1}">/emp-{1}.jsp</result>
<result name="delete" type="redirectAction">emp-list</result>
</action> </package>
</struts>

struts.xml

 <%@ 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=UTF-8">
<title>Insert title here</title>
</head>
<body> <a href="emp-list">List All Employees</a> </body>
</html>

index.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> <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>
<tbody>
<s:iterator value="#request.emps">
<tr>
<td>${employeeId}</td>
<td>${firstName}</td>
<td>${lastName}</td>
<td>${email}</td>
<td><a href="">Eidt</a></td>
<td><a href="emp-delete?employeeId=${employeeId }">Delete</a></td>
</tr>
</s:iterator>
</tbody> </table> </body>
</html>

emp-list.jsp

3.Struts2 运行流程图-1

  [原创]java WEB学习笔记65:Struts2 学习之路--Struts的CRUD操作( 查看 / 删除/ 添加)  ModelDriven拦截器  paramter 拦截器

4.Params 拦截器:

Parameters 拦截器将把表单字段映射到 ValueStack 栈的栈顶对象的各个属性中. 如果某个字段在模型里没有匹配的属性, Param 拦截器将尝试 ValueStack 栈中的下一个对象

[原创]java WEB学习笔记65:Struts2 学习之路--Struts的CRUD操作( 查看 / 删除/ 添加)  ModelDriven拦截器  paramter 拦截器

5.ModelDriven拦截器

  1)当用户触发 add 请求时, ModelDriven 拦截器将调用 EmployeeAction 对象的 getModel() 方法, 并把返回的模型(Employee实例)压入到 ValueStack 栈.

  2)接下来 Parameters 拦截器将把表单字段映射到 ValueStack 栈的栈顶对象的各个属性中. 因为此时 ValueStack 栈的栈顶元素是刚被压入的模型(Employee)对象, 所以该模型将被填充. 如果某个字段在模型里没有匹配的属性, Param 拦截器将尝试 ValueStack 栈中的下一个对象

  Action 实现ModelDriven 接口后的运行流程

 

 @Override
public String intercept(ActionInvocation invocation) throws Exception { //获取Action 对象:EmployeeAction对象,此时该Action 已经实现了ModelDriven 接口
//public class EmployeeAction implements RequestAware ,ModelDriven<EmployeeBean>
Object action = invocation.getAction();
//判断:action是否是ModelDriven 的实例
if (action instanceof ModelDriven) {
//强制转换 ModelDriven 类型
ModelDriven modelDriven = (ModelDriven) action;
//获取值栈
ValueStack stack = invocation.getStack();
//调用 ModelDriven 接口的getModel()方法
//即 调用 EmployeeAction 的 getModel()方法
/*
* @Override
public EmployeeBean getModel() {
employeeBean = new EmployeeBean();
return employeeBean;
} */ Object model = modelDriven.getModel();
if (model != null) {
//把getModel 的返回值压入到值栈的栈顶:实际压入的是employeeAction 的成员变量,employeeBean
stack.push(model);
}
if (refreshModelBeforeResult) {
invocation.addPreResultListener(new RefreshModelBeforeResult(modelDriven, model));
}
}
return invocation.invoke();
} //执行:PramaterInterception 的intercept() 方法:把请求的参数的值赋给栈顶对象,对应的属性,
//若栈顶对象没有对应的属性,则查询值栈中的下一个对象,直到找到此属性,赋值。 注意:getModel()方法不能如下实现
  因为 可以将此对象压入值栈的栈顶;但是当前的Action 成员变量是 null;
  public EmployeeBean getModel() {
  return new Employee();
}
 

  

实现modelDriven 接口

 package com.jason.strut2.curd;

 import java.util.Map;

 import org.apache.struts2.interceptor.RequestAware;

 import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.util.ValueStack; public class EmployeeAction implements RequestAware ,ModelDriven<EmployeeBean>{ private Dao dao = new Dao();
private Map<String, Object> requestMap;
private EmployeeBean employeeBean; // 需要在当前的 EmployeeAction 中定义employeeId 属性,以接收页面请求参数:删除操作 public String list() { requestMap.put("emps", dao.getEmployees());
return "list";
} //删除
public String delete() { dao.delete(employeeBean.getEmployeeId());
// 返回结果的类型应为 :redirectAction,也可以是chain:实际chain 是没有必要
// 还有,若使用chain 则到达目标页面后,地址栏显示的依然是 删除 的那个连接,刷新时 会有重复提交
return "success";
} public String save(){ //1.获取请求参数:通过定义属性的方式
//2.调用DAO的 svae 方法 dao.save(employeeBean);
//3.通过redirectAction 的方式响应结果给 emp-list
return "success";
} @Override
public void setRequest(Map<String, Object> requestMap) {
this.requestMap = requestMap;
} @Override
public EmployeeBean getModel() {
employeeBean = new EmployeeBean();
return employeeBean;
} }

public class EmployeeAction implements RequestAware ,ModelDriven

图示说明:

  [原创]java WEB学习笔记65:Struts2 学习之路--Struts的CRUD操作( 查看 / 删除/ 添加)  ModelDriven拦截器  paramter 拦截器

上一篇:微信支付文章综合


下一篇:安卓onTextChanged参数解释及实现EditText字数监听 Editable使用