Struts2的学习(4)-使用paramsPrepareParamsStack拦截器栈
2017-04-01 10:26
405 查看
使用paramsPrepareParamsStack拦截器栈:
(1) paramsPrepareParamsStack和defaultStack一样都是拦截器栈,而Struts-default包默认使用后者。
(2) 可以在struts配置文件中通过以下方式修改使用的默认的拦截器栈。
[html]
view plain
copy
print?
<constant name="struts.action.extension" value="action,do,"></constant>
<package name="default" namespace="/" extends="struts-default">
<!-- 配置使用paramsPrepareParamsStack作为默认的拦截器栈 -->
<default-interceptor-ref name="paramsPrepareParamsStack"></default-interceptor-ref>
<action name="emp-*" class="com.yu.struts2.app.EmployeeAction" method="{1}">
<result name="{1}">/{1}.jsp</result>
<result name="success" type="redirectAction">emp-list</result>
</action>
(3) paramsPrepareParamsStack拦截器栈在于:prams->modelDriven->params
所以可以先把请求参数赋给Action对应的属性,再根据赋给Action的那个属性决定压缩到值栈栈顶的对象,最后再为栈顶对象的属性赋值。
对于edit操作而言:
索要实现的效果:
其页面的代码list.jsp为:
[html]
view plain
copy
print?
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ 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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<s:debug></s:debug>
<s:form action="emp-save">
<s:textfield name="firstName" label<
1381f
span>="FirstName"></s:textfield>
<s:textfield name="lastName" label="LastName"></s:textfield>
<s:textfield name="email" label="Email"></s:textfield>
<s:submit></s:submit>
</s:form>
<br>
<br>
<br>
<table cellpadding="10" cellspacing="0", border="1">
<thead>
<td>ID</td>
<td>FirstName</td>
<td>LastName</td>
<td>Email</td>
<td>Edit</td>
<td>Delete</td>
</thead>
<tbody>
<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>
</tbody>
</table>
</body>
</html>
点击表格中的Edit进入edit.jsp页面进行回显并可更改:
edit.jsp的代码如下:
[html]
view plain
copy
print?
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ 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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<s:debug></s:debug>
<s: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>
</s:form>
</body>
</html>
EmployeeAction的实现代码如下:
[java]
view plain
copy
print?
package com.yu.struts2.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();
public String list(){
requestMap.put("emps", dao.getEmployees());
return "list";
}
public String delete(){
dao.delete(employeeId);
return "success";
}
private Employee employee;
public String save(){
// 1.获取请求参数:通过定义对应属性的方式
// 2.调用Dao的save方法
dao.save(employee);
return "success";
}
public String edit(){
// 1.获取传入的employeeId:employee.getEmployeeId()
// 2.根据employeeId获取Employee对象
//Employee emp = dao.get(employee.getEmployeeId());
// 3.把栈顶对象的属性转配好,此时栈顶对象时employee
// 目前的employee对象只有employeeId属性,其他属性为null
/*
* Struts2表单回显时:从值栈栈顶开始查找匹配的属性,若找到就添加到value属性中
*/
// employee.setEmail(emp.getEmail());
// employee.setFirstName(emp.getFirstName());
// employee.setLastName(emp.getLastName());
// 不能够进行表单的回显,因为经过重写赋值的employee对象不再是栈顶对象
//employee = dao.get(employee.getEmployeeId());
// 手动的把从数据库中获取的Employee对象放到值栈的栈顶。
// 但此时值栈栈顶及第二个对象均为Employee对象,不够完美
//ActionContext.getContext().getValueStack().push(dao.get(employee.getEmployeeId()));
return "edit";
}
public String update(){
dao.update(employee);
return "success";
}
private Map<String, Object> requestMap;
@Override
public void setRequest(Map<String, Object> arg0) {
// TODO Auto-generated method stub
this.requestMap = arg0;
}
private Integer employeeId;
public void setEmployeeId(Integer employeeId) {
this.employeeId = employeeId;
}
@Override
public Employee getModel() {
// 判断是Create还是Edit。
// 若为Create,则employee = new Employee();
// 若为Edit,则employee = dao.get(employeeId);
// 判定标准为是否有employeeId这个参数。若有该参数,则视为Edit;若没有该参数,则视为Create
// 若通过employeeId来判断,则需要在modelDriven拦截器之前执行一个params拦截器!
// 而这可以通过使用paramsPrepareParams拦截器栈实现
// 需要在struts.xml文件中配置使用paramsPrepareParams作为默认的拦截器栈
if(employeeId == null){
employee = new Employee();
}
else
employee = dao.get(employeeId);
return employee;
}
}
过程分析:
I、先为EmployeeAction的employeeId赋值
II、根据employeeId从数据库中加载对应的对象,并放入值栈栈顶;
III、再为栈顶对象的employeeId赋值(实际上此时employeeId属性值已经存在);
IV、把栈顶对象的属性回显在表单中
(4) 关于回显:struts表单标签会从值栈中获取对应的属性值进行回显。
(5) 存在的问题:
I、执行删除的时候,employeeId为null,但getModel方法却从数据库加载了一个对象,不该加载!
II、指向查询全部信息时,也new Employee()对象,浪费!
(1) paramsPrepareParamsStack和defaultStack一样都是拦截器栈,而Struts-default包默认使用后者。
(2) 可以在struts配置文件中通过以下方式修改使用的默认的拦截器栈。
[html]
view plain
copy
print?
<constant name="struts.action.extension" value="action,do,"></constant>
<package name="default" namespace="/" extends="struts-default">
<!-- 配置使用paramsPrepareParamsStack作为默认的拦截器栈 -->
<default-interceptor-ref name="paramsPrepareParamsStack"></default-interceptor-ref>
<action name="emp-*" class="com.yu.struts2.app.EmployeeAction" method="{1}">
<result name="{1}">/{1}.jsp</result>
<result name="success" type="redirectAction">emp-list</result>
</action>
<constant name="struts.action.extension" value="action,do,"></constant> <package name="default" namespace="/" extends="struts-default"> <!-- 配置使用paramsPrepareParamsStack作为默认的拦截器栈 --> <default-interceptor-ref name="paramsPrepareParamsStack"></default-interceptor-ref> <action name="emp-*" class="com.yu.struts2.app.EmployeeAction" method="{1}"> <result name="{1}">/{1}.jsp</result> <result name="success" type="redirectAction">emp-list</result> </action>
(3) paramsPrepareParamsStack拦截器栈在于:prams->modelDriven->params
所以可以先把请求参数赋给Action对应的属性,再根据赋给Action的那个属性决定压缩到值栈栈顶的对象,最后再为栈顶对象的属性赋值。
对于edit操作而言:
索要实现的效果:
其页面的代码list.jsp为:
[html]
view plain
copy
print?
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ 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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<s:debug></s:debug>
<s:form action="emp-save">
<s:textfield name="firstName" label<
1381f
span>="FirstName"></s:textfield>
<s:textfield name="lastName" label="LastName"></s:textfield>
<s:textfield name="email" label="Email"></s:textfield>
<s:submit></s:submit>
</s:form>
<br>
<br>
<br>
<table cellpadding="10" cellspacing="0", border="1">
<thead>
<td>ID</td>
<td>FirstName</td>
<td>LastName</td>
<td>Email</td>
<td>Edit</td>
<td>Delete</td>
</thead>
<tbody>
<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>
</tbody>
</table>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ 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=ISO-8859-1"> <title>Insert title here</title> </head> <body> <s:debug></s:debug> <s: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> </s:form> <br> <br> <br> <table cellpadding="10" cellspacing="0", border="1"> <thead> <td>ID</td> <td>FirstName</td> <td>LastName</td> <td>Email</td> <td>Edit</td> <td>Delete</td> </thead> <tbody> <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> </tbody> </table> </body> </html>
点击表格中的Edit进入edit.jsp页面进行回显并可更改:
edit.jsp的代码如下:
[html]
view plain
copy
print?
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ 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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<s:debug></s:debug>
<s: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>
</s:form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ 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=ISO-8859-1"> <title>Insert title here</title> </head> <body> <s:debug></s:debug> <s: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> </s:form> </body> </html>
EmployeeAction的实现代码如下:
[java]
view plain
copy
print?
package com.yu.struts2.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();
public String list(){
requestMap.put("emps", dao.getEmployees());
return "list";
}
public String delete(){
dao.delete(employeeId);
return "success";
}
private Employee employee;
public String save(){
// 1.获取请求参数:通过定义对应属性的方式
// 2.调用Dao的save方法
dao.save(employee);
return "success";
}
public String edit(){
// 1.获取传入的employeeId:employee.getEmployeeId()
// 2.根据employeeId获取Employee对象
//Employee emp = dao.get(employee.getEmployeeId());
// 3.把栈顶对象的属性转配好,此时栈顶对象时employee
// 目前的employee对象只有employeeId属性,其他属性为null
/*
* Struts2表单回显时:从值栈栈顶开始查找匹配的属性,若找到就添加到value属性中
*/
// employee.setEmail(emp.getEmail());
// employee.setFirstName(emp.getFirstName());
// employee.setLastName(emp.getLastName());
// 不能够进行表单的回显,因为经过重写赋值的employee对象不再是栈顶对象
//employee = dao.get(employee.getEmployeeId());
// 手动的把从数据库中获取的Employee对象放到值栈的栈顶。
// 但此时值栈栈顶及第二个对象均为Employee对象,不够完美
//ActionContext.getContext().getValueStack().push(dao.get(employee.getEmployeeId()));
return "edit";
}
public String update(){
dao.update(employee);
return "success";
}
private Map<String, Object> requestMap;
@Override
public void setRequest(Map<String, Object> arg0) {
// TODO Auto-generated method stub
this.requestMap = arg0;
}
private Integer employeeId;
public void setEmployeeId(Integer employeeId) {
this.employeeId = employeeId;
}
@Override
public Employee getModel() {
// 判断是Create还是Edit。
// 若为Create,则employee = new Employee();
// 若为Edit,则employee = dao.get(employeeId);
// 判定标准为是否有employeeId这个参数。若有该参数,则视为Edit;若没有该参数,则视为Create
// 若通过employeeId来判断,则需要在modelDriven拦截器之前执行一个params拦截器!
// 而这可以通过使用paramsPrepareParams拦截器栈实现
// 需要在struts.xml文件中配置使用paramsPrepareParams作为默认的拦截器栈
if(employeeId == null){
employee = new Employee();
}
else
employee = dao.get(employeeId);
return employee;
}
}
package com.yu.struts2.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(); public String list(){ requestMap.put("emps", dao.getEmployees()); return "list"; } public String delete(){ dao.delete(employeeId); return "success"; } private Employee employee; public String save(){ // 1.获取请求参数:通过定义对应属性的方式 // 2.调用Dao的save方法 dao.save(employee); return "success"; } public String edit(){ // 1.获取传入的employeeId:employee.getEmployeeId() // 2.根据employeeId获取Employee对象 //Employee emp = dao.get(employee.getEmployeeId()); // 3.把栈顶对象的属性转配好,此时栈顶对象时employee // 目前的employee对象只有employeeId属性,其他属性为null /* * Struts2表单回显时:从值栈栈顶开始查找匹配的属性,若找到就添加到value属性中 */ // employee.setEmail(emp.getEmail()); // employee.setFirstName(emp.getFirstName()); // employee.setLastName(emp.getLastName()); // 不能够进行表单的回显,因为经过重写赋值的employee对象不再是栈顶对象 //employee = dao.get(employee.getEmployeeId()); // 手动的把从数据库中获取的Employee对象放到值栈的栈顶。 // 但此时值栈栈顶及第二个对象均为Employee对象,不够完美 //ActionContext.getContext().getValueStack().push(dao.get(employee.getEmployeeId())); return "edit"; } public String update(){ dao.update(employee); return "success"; } private Map<String, Object> requestMap; @Override public void setRequest(Map<String, Object> arg0) { // TODO Auto-generated method stub this.requestMap = arg0; } private Integer employeeId; public void setEmployeeId(Integer employeeId) { this.employeeId = employeeId; } @Override public Employee getModel() { // 判断是Create还是Edit。 // 若为Create,则employee = new Employee(); // 若为Edit,则employee = dao.get(employeeId); // 判定标准为是否有employeeId这个参数。若有该参数,则视为Edit;若没有该参数,则视为Create // 若通过employeeId来判断,则需要在modelDriven拦截器之前执行一个params拦截器! // 而这可以通过使用paramsPrepareParams拦截器栈实现 // 需要在struts.xml文件中配置使用paramsPrepareParams作为默认的拦截器栈 if(employeeId == null){ employee = new Employee(); } else employee = dao.get(employeeId); return employee; } }
过程分析:
I、先为EmployeeAction的employeeId赋值
II、根据employeeId从数据库中加载对应的对象,并放入值栈栈顶;
III、再为栈顶对象的employeeId赋值(实际上此时employeeId属性值已经存在);
IV、把栈顶对象的属性回显在表单中
(4) 关于回显:struts表单标签会从值栈中获取对应的属性值进行回显。
(5) 存在的问题:
I、执行删除的时候,employeeId为null,但getModel方法却从数据库加载了一个对象,不该加载!
II、指向查询全部信息时,也new Employee()对象,浪费!
相关文章推荐
- Struts2的学习(4)-使用paramsPrepareParamsStack拦截器栈
- [原创]java WEB学习笔记66:Struts2 学习之路--Struts的CRUD操作( 查看 / 删除/ 添加) 使用 paramsPrepareParamsStack 重构代码 ,PrepareInterceptor拦截器,paramsPrepareParamsStack 拦截器栈
- Struts2 学习 第一章 struts2中Action类的使用
- Struts2学习(三):namespace属性的使用
- struts2学习笔记----result with params
- java struts2入门学习实例--使用struts2快速实现上传
- java struts2入门学习实例--使用struts进行验证
- Struts2学习(三):namespace属性的使用
- 【笔记】struts2学习笔记(二)--常见问题/一些配置/Myeclipse使用
- Maven学习(五)- 使用Maven构建Struts2项目
- Struts2使用通配符加载配置文件和通配符方法的底层实现学习
- struts2 学习总结(上传文件,解决乱码,通配符的使用,整合fck)
- Struts2学习(三):namespace属性的使用
- Struts2 - Controll Tag的使用(从零开始学习Strust2_06)
- Struts2 - 使用XML实现简单Validation(从零开始学习Strust2_05)
- 关于UtilTimerStack类的使用--XWork2、Struts2内置性能诊断类 --只为学习
- 学习笔记: Struts2中使用validate框架对Action的不同方法进行验证
- Struts2 - 在Tag中使用Properties属性文件(从零开始学习Strust2_04)
- 学习struts2建bbs总结五:使用jquery+ajax验证用户名是否存在以及struts效验信息不断重复的问题
- JAVA_WEB Struts2学习:使用验证框架进行校验JSP页面元素 反编译工具的配置 Strtuts2 国际化学习