您的位置:首页 > 编程语言 > Java开发

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>  



<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()对象,浪费!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐