[原創]java WEB學習筆記65:Struts2 學習之路--Struts的CRUD操做( 查看 / 刪除/ 添加) ModelDriven攔截器 paramter 攔截器

本博客的目的:①總結本身的學習過程,至關於學習筆記 ②將本身的經驗分享給你們,相互學習,互相交流,不可商用

內容不免出現問題,歡迎指正,交流,探討,能夠留言,也能夠經過如下方式聯繫。

本人互聯網技術愛好者,互聯網技術發燒友

微博:伊直都在0221

QQ:951226918

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------html

 

1.代碼結構java

2.代碼web

 

 1 package com.jason.strut2.curd;
 2 
 3 public class EmployeeBean {
 4 
 5     private Integer employeeId;
 6     private String firstNmame;
 7     private String lastName;
 8     private String email;
 9 
10     public Integer getEmployeeId() {
11         return employeeId;
12     }
13 
14     public void setEmployeeId(Integer employeeId) {
15         this.employeeId = employeeId;
16     }
17 
18     public String getFirstNmame() {
19         return firstNmame;
20     }
21 
22     public void setFirstNmame(String firstNmame) {
23         this.firstNmame = firstNmame;
24     }
25 
26     public String getLastName() {
27         return lastName;
28     }
29 
30     public void setLastName(String lastName) {
31         this.lastName = lastName;
32     }
33 
34     public String getEmail() {
35         return email;
36     }
37 
38     public void setEmail(String email) {
39         this.email = email;
40     }
41 
42     public EmployeeBean(Integer employeeId, String firstNmame, String lastName,
43             String email) {
44         super();
45         this.employeeId = employeeId;
46         this.firstNmame = firstNmame;
47         this.lastName = lastName;
48         this.email = email;
49     }
50 
51     public EmployeeBean() {
52         super();
53     }
54 
55 }
EmployeeBean

 

 1 package com.jason.strut2.curd;
 2 
 3 import java.util.ArrayList;
 4 import java.util.HashMap;
 5 import java.util.List;
 6 import java.util.Map;
 7 
 8 public class Dao {
 9 
10      private static Map<Integer, EmployeeBean> employees =  new HashMap<Integer, EmployeeBean>();
11      
12      static{
13              
14          employees.put(1001, new EmployeeBean(1001,"AA","aa","AA@163.com"));
15          employees.put(1002, new EmployeeBean(1002,"BB","bb","BB@163.com"));
16          employees.put(1003, new EmployeeBean(1003,"CC","cc","CC@163.com"));
17          employees.put(1004, new EmployeeBean(1004,"DD","dd","DD@163.com"));
18          employees.put(1005, new EmployeeBean(1005,"EE","ee","EE@163.com"));
19          
20      }
21      
22      
23      //查看
24      public List<EmployeeBean> getEmployees(){
25          return new ArrayList<>(employees.values());
26      }
27      
28      //刪除
29      public void delete(Integer id){
30          employees.remove(id);
31      }
32      
33      //保存
34      public void save(EmployeeBean employeeBean){
35          long time = System.currentTimeMillis();
36          employeeBean.setEmployeeId((int) time);
37          employees.put(employeeBean.getEmployeeId(), employeeBean);
38          
39      }
40      
41      //獲取
42      public EmployeeBean get(Integer employeeId){
43          
44          return  employees.get(employeeId);
45      }
46      
47      //更新
48      public void update(EmployeeBean employeeBean){
49          employees.put(employeeBean.getEmployeeId(), employeeBean);
50      }
51     
52 }
Dao
 1 package com.jason.strut2.curd;
 2 
 3 import java.util.Map;
 4 
 5 import org.apache.struts2.interceptor.RequestAware;
 6 
 7 public class EmployeeAction implements RequestAware {
 8 
 9     private Dao dao = new Dao();
10     private Map<String, Object> requestMap;
11 
12     // 須要在當前的 EmployeeAction 中定義employeeId 屬性,以接收頁面請求參數:刪除操做
13     private Integer employeeId;
14 
15     public String list() {
16 
17         requestMap.put("emps", dao.getEmployees());
18         return "list";
19     }
20 
21     public String delete() {
22 
23         dao.delete(employeeId);
24         // 返回結果的類型應爲 :redirectAction,也能夠是chain:實際chain 是沒有必要
25         // 還有,若使用chain 則到達目標頁面後,地址欄顯示的依然是 刪除 的那個鏈接,刷新時 會有重複提交
26         return "delete";
27     }
28 
29     @Override
30     public void setRequest(Map<String, Object> requestMap) {
31         this.requestMap = requestMap;
32     }
33 
34     public Integer getEmployeeId() {
35         return employeeId;
36     }
37 
38     public void setEmployeeId(Integer employeeId) {
39         this.employeeId = employeeId;
40     }
41 
42 }
EmployeeAction

 

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10     
11     <a href="emp-list">List All Employees</a>
12 
13 </body>
14 </html>
web.xml

 

  

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 4     "http://struts.apache.org/dtds/struts-2.3.dtd">
 5 
 6 <struts>
 7 
 8      <constant name="struts.action.extension" value="action,do,"></constant>
 9      <package name="default" namespace="/" extends="struts-default">
10          
11          <action name="emp-*" class="com.jason.strut2.curd.EmployeeAction" method="{1}">
12              <result name="{1}">/emp-{1}.jsp</result>
13              <result name="delete" type="redirectAction">emp-list</result>
14          </action>
15          
16          
17      </package>
18 </struts>
struts.xml

 

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10     
11     <a href="emp-list">List All Employees</a>
12 
13 </body>
14 </html>
index.jsp
 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3     
 4  <%@ taglib prefix="s" uri="/struts-tags" %>
 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 6 <html>
 7 <head>
 8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 9 <title>Insert title here</title>
10 </head>
11 <body>
12 
13     <table cellpadding="10" cellspacing="0" border="1">
14         <thead>
15             <tr>
16                 <td>ID</td>
17                 <td>FirstName</td>
18                 <td>LastName</td>
19                 <td>Email</td>
20                 <td>Edit</td>
21                 <td>Delete</td>
22             </tr>
23         </thead>
24         <tbody>
25             <s:iterator  value="#request.emps">
26                 <tr>
27                     <td>${employeeId}</td>
28                     <td>${firstName}</td>
29                     <td>${lastName}</td>
30                     <td>${email}</td>
31                     <td><a href="">Eidt</a></td>
32                     <td><a href="emp-delete?employeeId=${employeeId }">Delete</a></td>
33                 </tr>
34             </s:iterator>
35         </tbody>
36         
37     </table>
38 
39 
40 </body>
41 </html>
emp-list.jsp

 

 

3.Struts2 運行流程圖-1apache

  

 

4.Params 攔截器:jsp

Parameters 攔截器將把表單字段映射到 ValueStack 棧的棧頂對象的各個屬性中. 若是某個字段在模型裏沒有匹配的屬性, Param 攔截器將嘗試 ValueStack 棧中的下一個對象ide

 

 

 

 

5.ModelDriven攔截器學習

  1)當用戶觸發 add 請求時, ModelDriven 攔截器將調用 EmployeeAction 對象的 getModel() 方法, 並把返回的模型(Employee實例)壓入到 ValueStack 棧.ui

  2)接下來 Parameters 攔截器將把表單字段映射到 ValueStack 棧的棧頂對象的各個屬性中. 由於此時 ValueStack 棧的棧頂元素是剛被壓入的模型(Employee)對象, 因此該模型將被填充. 若是某個字段在模型裏沒有匹配的屬性, Param 攔截器將嘗試 ValueStack 棧中的下一個對象this

 

  Action 實現ModelDriven 接口後的運行流程spa

 

 1 @Override  2         public String intercept(ActionInvocation invocation) throws Exception {  3          
 4         //獲取Action 對象:EmployeeAction對象,此時該Action 已經實現了ModelDriven 接口  5          //public class EmployeeAction implements RequestAware ,ModelDriven<EmployeeBean>
 6             Object action = invocation.getAction();  7            //判斷:action是不是ModelDriven 的實例
 8             if (action instanceof ModelDriven) {  9                 //強制轉換 ModelDriven 類型
10                 ModelDriven modelDriven = (ModelDriven) action; 11                 //獲取值棧
12                 ValueStack stack = invocation.getStack(); 13                 //調用 ModelDriven 接口的getModel()方法 14                  //即 調用 EmployeeAction 的 getModel()方法
15                 /*
16  * @Override 17  public EmployeeBean getModel() { 18  employeeBean = new EmployeeBean(); 19  return employeeBean; 20  } 21 
22                  */
23                     
24                 Object model = modelDriven.getModel(); 25                 if (model !=  null) { 26                     //把getModel 的返回值壓入到值棧的棧頂:實際壓入的是employeeAction 的成員變量,employeeBean
27  stack.push(model); 28  } 29                 if (refreshModelBeforeResult) { 30                     invocation.addPreResultListener(new RefreshModelBeforeResult(modelDriven, model)); 31  } 32  } 33             return invocation.invoke(); 34  } 35      
36      
37      //執行:PramaterInterception 的intercept() 方法:把請求的參數的值賦給棧頂對象,對應的屬性, 38      //若棧頂對象沒有對應的屬性,則查詢值棧中的下一個對象,直到找到此屬性,賦值。




注意:getModel()方法不能以下實現
  由於 能夠將此對象壓入值棧的棧頂;可是當前的Action 成員變量是 null;
  public EmployeeBean getModel() {
  return new Employee();
}
 

 

  

實現modelDriven 接口

 1 package com.jason.strut2.curd;
 2 
 3 import java.util.Map;
 4 
 5 import org.apache.struts2.interceptor.RequestAware;
 6 
 7 import com.opensymphony.xwork2.ActionInvocation;
 8 import com.opensymphony.xwork2.ModelDriven;
 9 import com.opensymphony.xwork2.util.ValueStack;
10 
11 public class EmployeeAction implements RequestAware ,ModelDriven<EmployeeBean>{
12 
13     private Dao dao = new Dao();
14     private Map<String, Object> requestMap;
15     private EmployeeBean employeeBean;
16 
17     // 須要在當前的 EmployeeAction 中定義employeeId 屬性,以接收頁面請求參數:刪除操做
18     
19 
20     public String list() {
21 
22         requestMap.put("emps", dao.getEmployees());
23         return "list";
24     }
25 
26     //刪除
27     public String delete() {
28 
29         dao.delete(employeeBean.getEmployeeId());
30         // 返回結果的類型應爲 :redirectAction,也能夠是chain:實際chain 是沒有必要
31         // 還有,若使用chain 則到達目標頁面後,地址欄顯示的依然是 刪除 的那個鏈接,刷新時 會有重複提交
32         return "success";
33     }
34 
35     
36     public String save(){
37         
38             //1.獲取請求參數:經過定義屬性的方式
39             //2.調用DAO的 svae 方法
40     
41             dao.save(employeeBean);
42             //3.經過redirectAction 的方式響應結果給 emp-list
43             return "success";
44     }
45     
46     @Override
47     public void setRequest(Map<String, Object> requestMap) {
48         this.requestMap = requestMap;
49     }
50 
51     
52 
53     @Override
54     public EmployeeBean getModel() {
55           employeeBean  = new EmployeeBean();
56         return employeeBean;
57     }
58 
59 }
public class EmployeeAction implements RequestAware ,ModelDriven

 

 

圖示說明:

  

相關文章
相關標籤/搜索