Struts2的學習(4)-使用paramsPrepareParamsStack攔截器棧

使用paramsPrepareParamsStack攔截器棧:
(1) paramsPrepareParamsStack和defaultStack一樣都是攔截器棧,而Struts-default包默認使用後者。
(2) 可以在struts配置文件中通過以下方式修改使用的默認的攔截器棧。
  1. <constant name="struts.action.extension" value="action,do,"></constant>  
  2.     <package name="default" namespace="/" extends="struts-default">  
  3.   
  4.         <!-- 配置使用paramsPrepareParamsStack作爲默認的攔截器棧 -->  
  5.         <default-interceptor-ref name="paramsPrepareParamsStack"></default-interceptor-ref>  
  6.   
  7.         <action name="emp-*" class="com.yu.struts2.app.EmployeeAction" method="{1}">  
  8.             <result name="{1}">/{1}.jsp</result>  
  9.             <result name="success" type="redirectAction">emp-list</result>  
  10.         </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爲:
  1. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"  
  2.     pageEncoding="ISO-8859-1"%>  
  3. <%@ taglib prefix="s" uri="/struts-tags" %>  
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  5. <html>  
  6. <head>  
  7. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
  8. <title>Insert title here</title>  
  9. </head>  
  10. <body>  
  11.   
  12.     <s:debug></s:debug>  
  13.   
  14.     <s:form action="emp-save">  
  15.         <s:textfield name="firstName" label="FirstName"></s:textfield>  
  16.         <s:textfield name="lastName" label="LastName"></s:textfield>  
  17.         <s:textfield name="email" label="Email"></s:textfield>  
  18.         <s:submit></s:submit>  
  19.     </s:form>  
  20.   
  21.     <br>  
  22.     <br>  
  23.     <br>  
  24.     <table cellpadding="10" cellspacing="0"border="1">  
  25.         <thead>  
  26.             <td>ID</td>  
  27.             <td>FirstName</td>  
  28.             <td>LastName</td>  
  29.             <td>Email</td>  
  30.             <td>Edit</td>  
  31.             <td>Delete</td>  
  32.         </thead>  
  33.   
  34.         <tbody>  
  35.             <s:iterator value="#request.emps">  
  36.                 <tr>  
  37.                     <td>${employeeId }</td>  
  38.                     <td>${firstName }</td>  
  39.                     <td>${lastName }</td>  
  40.                     <td>${email }</td>  
  41.                     <td><a href="emp-edit?employeeId=${employeeId }">Edit</a></td>  
  42.                     <td><a href="emp-delete?employeeId=${employeeId }">Delete</a></td>  
  43.                 </tr>  
  44.             </s:iterator>  
  45.         </tbody>  
  46.   
  47.     </table>  
  48. </body>  
  49. </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的代碼如下:
  1. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"  
  2.     pageEncoding="ISO-8859-1"%>  
  3. <%@ taglib prefix="s" uri="/struts-tags" %>  
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  5. <html>  
  6. <head>  
  7. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
  8. <title>Insert title here</title>  
  9. </head>  
  10. <body>  
  11.   
  12.     <s:debug></s:debug>  
  13.   
  14.     <s:form action="emp-update">  
  15.   
  16.         <s:hidden name="employeeId"></s:hidden>  
  17.   
  18.         <s:textfield name="firstName" label="FirstName" ></s:textfield>  
  19.         <s:textfield name="lastName" label="LastName" ></s:textfield>  
  20.         <s:textfield name="email" label="Email" ></s:textfield>  
  21.         <s:submit></s:submit>  
  22.     </s:form>  
  23.   
  24. </body>  
  25. </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的實現代碼如下:
  1. package com.yu.struts2.app;  
  2.   
  3. import java.util.Map;  
  4.   
  5. import org.apache.struts2.interceptor.RequestAware;  
  6.   
  7. import com.opensymphony.xwork2.ActionContext;  
  8. import com.opensymphony.xwork2.ModelDriven;  
  9.   
  10. public class EmployeeAction implements RequestAware ,ModelDriven<Employee>{  
  11.   
  12.     private Dao dao = new Dao();  
  13.   
  14.     public String list(){  
  15.         requestMap.put("emps", dao.getEmployees());  
  16.         return "list";  
  17.     }  
  18.   
  19.     public String delete(){  
  20.         dao.delete(employeeId);  
  21.         return "success";  
  22.     }  
  23.   
  24.     private Employee employee;  
  25.   
  26.     public String save(){  
  27.         // 1.獲取請求參數:通過定義對應屬性的方式  
  28.         // 2.調用Dao的save方法  
  29.         dao.save(employee);  
  30.   
  31.         return "success";  
  32.     }  
  33.   
  34.     public String edit(){  
  35.         // 1.獲取傳入的employeeId:employee.getEmployeeId()  
  36.         // 2.根據employeeId獲取Employee對象  
  37.         //Employee emp = dao.get(employee.getEmployeeId());  
  38.   
  39.         // 3.把棧頂對象的屬性轉配好,此時棧頂對象時employee  
  40.         // 目前的employee對象只有employeeId屬性,其他屬性爲null  
  41.         /* 
  42.          *  Struts2表單回顯時:從值棧棧頂開始查找匹配的屬性,若找到就添加到value屬性中 
  43.          */  
  44. //        employee.setEmail(emp.getEmail());  
  45. //        employee.setFirstName(emp.getFirstName());  
  46. //        employee.setLastName(emp.getLastName());  
  47.   
  48.         // 不能夠進行表單的回顯,因爲經過重寫賦值的employee對象不再是棧頂對象  
  49.         //employee = dao.get(employee.getEmployeeId());  
  50.   
  51.         // 手動的把從數據庫中獲取的Employee對象放到值棧的棧頂。  
  52.         // 但此時值棧棧頂及第二個對象均爲Employee對象,不夠完美  
  53.         //ActionContext.getContext().getValueStack().push(dao.get(employee.getEmployeeId()));  
  54.   
  55.         return "edit";  
  56.     }  
  57.   
  58.     public String update(){  
  59.         dao.update(employee);  
  60.   
  61.         return "success";  
  62.     }  
  63.   
  64.     private Map<String, Object> requestMap;  
  65.   
  66.     @Override  
  67.     public void setRequest(Map<String, Object> arg0) {  
  68.         // TODO Auto-generated method stub  
  69.         this.requestMap = arg0;  
  70.     }  
  71.   
  72.     private Integer employeeId;  
  73.   
  74.     public void setEmployeeId(Integer employeeId) {  
  75.         this.employeeId = employeeId;  
  76.     }  
  77.   
  78.     @Override  
  79.     public Employee getModel() {  
  80.         // 判斷是Create還是Edit。  
  81.         // 若爲Create,則employee = new Employee();  
  82.         // 若爲Edit,則employee = dao.get(employeeId);  
  83.         // 判定標準爲是否有employeeId這個參數。若有該參數,則視爲Edit;若沒有該參數,則視爲Create  
  84.         // 若通過employeeId來判斷,則需要在modelDriven攔截器之前執行一個params攔截器!  
  85.         // 而這可以通過使用paramsPrepareParams攔截器棧實現  
  86.         // 需要在struts.xml文件中配置使用paramsPrepareParams作爲默認的攔截器棧  
  87.   
  88.         if(employeeId == null){  
  89.             employee = new Employee();  
  90.         }  
  91.         else  
  92.             employee = dao.get(employeeId);  
  93.   
  94.         return employee;  
  95.     }  
  96. }  
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()對象,浪費!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章