主要笔记:
2. 使用 paramsPrepareParamsStack 拦截器栈后的运行流程
1). paramsPrepareParamsStack 和 defaultStack 一样都是拦截器栈. 而 struts-default 包默认使用的是
defaultStack
2). 可以在 Struts 配置文件中通过以下方式修改使用的默认的拦截器栈
3). paramsPrepareParamsStack 拦截器在于
params -> modelDriven -> params
所以可以先把请求参数赋给 Action 对应的属性, 再根据赋给 Action 的那个属性值决定压到值栈栈顶的对象, 最后再为栈顶对象的属性赋值.
对于 edit 操作而言:
I. 先为 EmployeeAction 的 employeeId 赋值
II. 根据 employeeId 从数据库中加载对应的对象, 并放入到值栈的栈顶
III. 再为栈顶对象的 employeeId 赋值(实际上此时 employeeId 属性值已经存在)
IV. 把栈顶对象的属性回显在表单中.
4). 关于回显: Struts2 表单标签会从值栈中获取对应的属性值进行回显.
5). 存在的问题:
getModel 方法
public Employee getModel() {
if(employeeId == null)
employee = new Employee();
else
employee = dao.get(employeeId);
return employee;
}
I. 在执行删除的时候, employeeId 不为 null, 但 getModel 方法却从数据库加载了一个对象. 不该加载!
II. 指向查询全部信息时, 也 new Employee() 对象. 浪费!
6). 解决方案: 使用 PrepareInterceptor 和 Preparable 接口.
7). 关于 PrepareInterceptor
[分析后得到的结论]
若 Action 实现了 Preparable 接口, 则 Struts 将尝试执行 prepare[ActionMethodName] 方法,
若 prepare[ActionMethodName] 不存在, 则将尝试执行 prepareDo[ActionMethodName] 方法.
若都不存在, 就都不执行.
若 PrepareInterceptor 的 alwaysInvokePrepare 属性为 false,
则 Struts2 将不会调用实现了 Preparable 接口的 Action 的 prepare() 方法
[能解决 5) 的问题的方案]
可以为每一个 ActionMethod 准备 prepare[ActionMethdName] 方法, 而抛弃掉原来的 prepare() 方法
将 PrepareInterceptor 的 alwaysInvokePrepare 属性置为 false, 以避免 Struts2 框架再调用 prepare() 方法.
如何在配置文件中为拦截器栈的属性赋值: 参看 /struts-2.3.15.3/docs/WW/docs/interceptors.html
token
———————————-源代码解析———————————
public String doIntercept(ActionInvocation invocation) throws Exception {
//获取 Action 实例
Object action = invocation.getAction();
//判断 Action 是否实现了 Preparable 接口
if (action instanceof Preparable) {
try {
String[] prefixes;
//根据当前拦截器的 firstCallPrepareDo(默认为 false) 属性确定 prefixes
if (firstCallPrepareDo) {
prefixes = new String[] {ALT_PREPARE_PREFIX, PREPARE_PREFIX};
} else {
prefixes = new String[] {PREPARE_PREFIX, ALT_PREPARE_PREFIX};
}
//若为 false, 则 prefixes: prepare, prepareDo
//调用前缀方法.
PrefixMethodInvocationUtil.invokePrefixMethod(invocation, prefixes);
}
catch (InvocationTargetException e) {
Throwable cause = e.getCause();
if (cause instanceof Exception) {
throw (Exception) cause;
} else if(cause instanceof Error) {
throw (Error) cause;
} else {
throw e;
}
}
//根据当前拦截器的 alwaysInvokePrepare(默认是 true) 决定是否调用 Action 的 prepare 方法
if (alwaysInvokePrepare) {
((Preparable) action).prepare();
}
}
return invocation.invoke();
}
PrefixMethodInvocationUtil.invokePrefixMethod(invocation, prefixes) 方法:
public static void invokePrefixMethod(ActionInvocation actionInvocation, String[] prefixes) throws InvocationTargetException, IllegalAccessException {
//获取 Action 实例
Object action = actionInvocation.getAction();
//获取要调用的 Action 方法的名字(update)
String methodName = actionInvocation.getProxy().getMethod();
if (methodName == null) {
// if null returns (possible according to the docs), use the default execute
methodName = DEFAULT_INVOCATION_METHODNAME;
}
//获取前缀方法
Method method = getPrefixedMethod(prefixes, methodName, action);
//若方法不为 null, 则通过反射调用前缀方法
if (method != null) {
method.invoke(action, new Object[0]);
}
}
PrefixMethodInvocationUtil.getPrefixedMethod 方法:
public static Method getPrefixedMethod(String[] prefixes, String methodName, Object action) {
assert(prefixes != null);
//把方法的首字母变为大写
String capitalizedMethodName = capitalizeMethodName(methodName);
//遍历前缀数组
for (String prefixe : prefixes) {
//通过拼接的方式, 得到前缀方法名: 第一次 prepareUpdate, 第二次 prepareDoUpdate
String prefixedMethodName = prefixe + capitalizedMethodName;
try {
//利用反射获从 action 中获取对应的方法, 若有直接返回. 并结束循环.
return action.getClass().getMethod(prefixedMethodName, EMPTY_CLASS_ARRAY);
}
catch (NoSuchMethodException e) {
// hmm -- OK, try next prefix
if (LOG.isDebugEnabled()) {
LOG.debug("cannot find method [#0] in action [#1]", prefixedMethodName, action.toString());
}
}
}
return null;
}
- Action 实现 ModelDriven 接口后的运行流程
1). 先会执行 ModelDrivenInterceptor 的 intercept 方法.
public String intercept(ActionInvocation invocation) throws Exception {
//获取 Action 对象: EmployeeAction 对象, 此时该 Action 已经实现了 ModelDriven 接口
//public class EmployeeAction implements RequestAware, ModelDriven<Employee>
Object action = invocation.getAction();
//判断 action 是否是 ModelDriven 的实例
if (action instanceof ModelDriven) {
//强制转换为 ModelDriven 类型
ModelDriven modelDriven = (ModelDriven) action;
//获取值栈
ValueStack stack = invocation.getStack();
//调用 ModelDriven 接口的 getModel() 方法
//即调用 EmployeeAction 的 getModel() 方法
/*
public Employee getModel() {
employee = new Employee();
return employee;
}
*/
Object model = modelDriven.getModel();
if (model != null) {
//把 getModel() 方法的返回值压入到值栈的栈顶. 实际压入的是 EmployeeAction 的 employee 成员变量
stack.push(model);
}
if (refreshModelBeforeResult) {
invocation.addPreResultListener(new RefreshModelBeforeResult(modelDriven, model));
}
}
return invocation.invoke();
}
2). 执行 ParametersInterceptor 的 intercept 方法: 把请求参数的值赋给栈顶对象对应的属性. 若栈顶对象没有对应的属性, 则查询
值栈中下一个对象对应的属性…
3). 注意: getModel 方法不能提供以下实现. 的确会返回一个 Employee 对象到值栈的栈顶. 但当前 Action
的 employee 成员变量却是 null.
public Employee getModel() {
return new Employee();
}
struts.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>
<!-- 配置struts2的默认主题
simple: 把 UI 标签翻译成最简单的 HTML 对应元素, 而且会忽视行标属性
xhtml: xhtml 是默认的主题. 这个主题的模板通过使用一个布局表格提供了一种自动化的排版机制.
css_xhtml: 这个主题里的模板与 xhtml 主题里的模板很相似, 但它们将使用 css 来进行布局和排版
ajax: 这个主题里的模板以 xhtml 主题里德模板为基础, 但增加了一些 Ajax 功能.
-->
<constant name="struts.ui.theme" value="xhtml"></constant>
<!-- 配置 Struts 可以受理的请求的扩展名 -->
<constant name="struts.action.extension" value="action,do"></constant>
<!-- 打开允许调用静态方法的开关 -->
<constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>
<!-- 打开允许动态方法调用的开关, 默认是 false -->
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
<package name="employee-action" extends="struts-default">
<!-- 配置使用 paramsPrepareParamsStack 作为默认的拦截器栈 -->
<!-- 修改 PrepareInterceptor 拦截器的 alwaysInvokePrepare 属性值为 false -->
<interceptors>
<interceptor-stack name="hghstack">
<interceptor-ref name="paramsPrepareParamsStack">
<param name="prepare.alwaysInvokePrepare">false</param>
</interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="hghstack"/>
<action name="emps-list" class="com.hgh.struts25.app.EmployeeAction" method="list">
<result name="list">/emps-list.jsp</result>
</action>
<action name="emps-*" class="com.hgh.struts25.app.EmployeeAction" method="{1}">
<result name="{1}">/emps-{1}.jsp</result>
<result name="success" type="redirectAction">emps-list</result>
</action>
</package>
</struts>
emps-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>
<s:form action="emps-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 name="submit"></s:submit>
</s:form>
<table cellspacing="0" cellpadding="10" border="1">
<thead>
<tr>
<td>employeeId</td>
<td>firstName</td>
<td>lastName</td>
<td>email</td>
</tr>
</thead>
<tbody>
<s:iterator value="#request.emps" var="employee">
<tr>
<td>${employee.employeeId }</td>
<td>${employee.firstName }</td>
<td>${employee.lastName }</td>
<td>${employee.email }</td>
<!--
http://localhost:8080/struts2-5/emps-list.action
请求路径:/package中namespace的属性值/action中name的属性值.拓展名?请求参数
-->
<td><a href="emps-edit.action?employeeId=${employeeId }">Edit</a></td>
<td><a href="emps-delete.action?employeeId=${employeeId }">Delete</a></td>
</tr>
</s:iterator>
</tbody>
</table>
</body>
</html>
emps-edit.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>
<s:form action="emps-save">
<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 name="submit"></s:submit>
</s:form>
</body>
</html>
EmployeeAction
package com.hgh.struts25.app;
import java.util.List;
import java.util.Map;
import org.apache.struts2.interceptor.RequestAware;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
public class EmployeeAction implements RequestAware, ModelDriven<Employee>,Preparable{
private Map<String, Object> requestMap;
private EmployeeDao dao = new EmployeeDao();
private Employee employee;
public String list(){
List<Employee> employees = dao.getEmployees();
requestMap.put("emps", employees);
return "list";
}
public void prepareUpdate(){
employee = new Employee();
}
public void prepareEdit(){
employee = dao.get(employeeId);
}
public void prepareSave(){
employee = new Employee();
}
public String delete(){
dao.delete(employeeId);
return "success";
}
public String save(){
dao.save(employee);
return "success";
}
public String edit(){
// Employee employee2 = dao.get(employee.getEmployeeId());
//
// employee.setEmail(employee2.getEmail());
// employee.setFirstName(employee2.getFirstName());
// employee.setLastName(employee2.getLastName());
return "edit";
}
@Override
public void setRequest(Map<String, Object> arg0) {
// TODO Auto-generated method stub
this.requestMap = arg0;
}
private Integer employeeId;
public Integer getEmployeeId() {
return employeeId;
}
public void setEmployeeId(Integer employeeId) {
this.employeeId = employeeId;
}
/**
*
* 在action类中需要注入整个pojo的所有属性太麻烦,所以要实现:ModelDriven接口,该接口会把getModel()方法的返回值压入栈顶,所以也没接收到的属性值就可以复制给employee对象了
*
* 但是感觉该方法也有缺陷,如果我有很多个对象都想接收页面的返回值和回显页面
*/
@Override
public Employee getModel() {
// TODO Auto-generated method stub
//employee = new Employee();
/*
* 通过在model中,使用prepare拦截器,对employeeId值是否存在做判断,然后再决定是create还是update,delete,query
* 不过还是有点问题这种方式。
*/
//实现了prepare接口的方法之后,看不需要下面这些了
// if (null == employeeId) {
// employee = new Employee();
// }else {
// employee = dao.get(employeeId);
// }
return employee;
}
@Override
public void prepare() throws Exception {
// TODO Auto-generated method stub
System.out.println("prepare.....");
}
}
运行在List上面点击edit的方法调用流程
1.先调用setEmployeeId
2.再调用setRequest
3. 调用prepareEdit
public void prepareEdit(){
employee = dao.get(employeeId);
}
4.
@Override
public Employee getModel() {
return employee;
}
5.
public String edit(){
return "edit";
}
6.
public Integer getEmployeeId() {
return employeeId;
}
7.
@Override
public Employee getModel() {
return employee;
}