(三)Struts2數據處理

Struts2框架框架使用OGNL語言和值棧技術實現數據的流轉處理。

 值棧就相當於一個容器,用來存放數據,而OGNL是一種快速查詢數據的語言。

 

 值棧:ValueStack一種數據結構,操作數據的方式爲:先進後出

 OGNL : Object-GraphNavigation Language(對象圖形導航語言)將多個對象的關係使用一種樹形的結構展現出來,更像一個圖形,那麼如果需要對樹形結構的節點數據進行操作,那麼可以使用 對象.屬性 的方式進行操作,OGNL技術底層採用反射實現。

 

 

一:數據的提交方式

1,<form>表單提交

<form action="helloWorld" method="post">      
    用戶名:</td><td><input type="text" class="message" name="username"><br>   
    密  碼:  </td><td><input class="message" type="password" name="userpassword"><br> 
    <input type="submit" value="登錄"><input type="reset" value="重置"></td></tr>      
   </form> 


2,url的方式  

[html] view plain copy
  1. http://localhost.egov.com/user/login_Login.action?usercode=admin&userpswd=admin  
 

3,超鏈接

[html] view plain copy
  1. <a href="/user/delete.action?usercode=admin">刪除</a>    


4,異步提交

 ajax異步提交數據

 

二:數據存儲

 數據的存儲依賴於框架提供的攔截器功能,攔截器可以對請求進行攔截,獲取所有的請求參數,循環遍歷設置到值棧中。框架默認將被請求的Action對象存放到值棧的棧頂。Struts2框架提供三種方式將參數存入值棧


1,屬性驅動模式

 Action中需要提供參數名稱的set方法,框架會通過攔截器將請求參數獲取到之後,會循環遍歷將參數設置到值棧(棧頂對象)中。

[html] view plain copy
  1. <input name="usercode" type="text" >  
  2. Ognl.setValue("usercode",action,"admin");//此時根對象是action  
  3.    


2,模型驅動模式

 必須要實現模型驅動接口,屬於侵入性開發方式,不推薦使用

 需要定義數據模型的類,將屬性封裝到數據模型類中,Action中只需要定義模型對象的類型屬性(必須創建對象賦值),模型對象的get/set屬性並不是必須的。

 Action類需要實現ModelDriven接口,重寫getModel()方法。

[html] view plain copy
  1. <inputnameinputname="usercode" type="text" ><br>表單依然是模型對象的屬性名稱作爲參數名稱  
  2. Ognl.setValue("usercode",user,"admin");//此時根(棧頂)對象是user對象  
  3.    

3,域驅動模式

 與屬性驅動的原理是類似,也是通過參數攔截器,將請求參數獲取後循環設置到值棧中。

 在Action對象中定義模型對象屬性,並提供get/set方法;

 在表單元素中增加模型對象屬性的名稱;

[html] view plain copy
  1. <inputnameinputname="user.usercode" type="text" >  
  2. Ognl.setValue("user.usercode",action,"admin");//此時根對象是action  
  3.    

 

三:數據的傳遞

 框架將Http對象和包裝後的Map集合對象都存放到ActionContext上下文對象集合中。所以可以根據ActionContext來獲取想要使用的對象。

 

1,獲取HTTP對象

[html] view plain copy
  1. ActionContext ac = ActionContext.getContext(); //上下文對象相當於request範圍  
  2. HttpServletRequest request =(HttpServletRequest)ac.get(StrutsStatics.HTTP_REQUEST);  
  3.   
  4. HttpSession session = request.getSession(false);  
  5.   
  6. ServletContext application = session.getServletContext();  
  7. ServletContext application = ac.get(StrutsStatics.SERVLET_CONTEXT);  
  8.   
  9. HttpServletRequest request = ServletActionContext.getRequest(); (推薦使用)  
  10. HttpServletResponse response =ServletActionContext.getResponse();  
  11.   
  12. Action類實現ServletRequestAware,或ServletResponseAware (屬於侵入性開發方式,不推薦使用)  
  13.   
  14. private HttpServletRequest request  ; //set注入  
  15. private HttpServletResponse response ;  
  16.   
  17. @Override  
  18. public void setServletRequest(HttpServletRequest request) {  //實現該方法,該方法由框架調用,傳遞參數。  
  19.      this.requestrequest ;  
  20. }  
  21.   
  22. @Override  
  23. public void setServletResponse(HttpServletResponse response) {  
  24.     this.responseresponse ;  
  25. }  


2,獲取Map集合

[html] view plain copy
  1. ActionContext ac = ActionContext.getContext();  
  2.   
  3. Mapsession = ac.getSession();  
  4. Mapsession2 =(Map)ac.get("session");  
  5. Mapsession3=(Map)ac.get(ActionContext.SESSION);          
  6.   
  7. Mapapplication = ac.getApplication()  
  8. Mapapplication = ac.get(ActionContext.APPLICATION);  
  9. Mapapplication = ac.get("application");  


3, 獲取值棧對象以及參數集合對象

[html] view plain copy
  1. ActionContext ac = ActionContext.getContext();  
  2. ValueStack vs = ac.getValueStack();  
  3.   
  4. Map paramts = ac.getParameters();  

四:數據的顯示

 用El表達式的形式,取request對象中的值

[html] view plain copy
  1. ${requestScope.username }  
  2.   
  3. 而這種表達式的方式可以表示成java代碼的方式:  
  4.   
  5. <%  
  6.   Stringusername =(String)request.getAttribute("username");//被重寫的方法,底層是通過ognl查詢值棧中的數據  
  7.   out.print(username);  
  8. %>    

 從request返回取數據,實質上底層是通過Ognl語言操作值棧中的數據。 ActionContext對象的集合和OgnlValueStack值棧的集合爲同一個集合對象,所以從值棧的Map集合中取數據,就相當於從ActionContext上下文中取數據。


struts案例:https://github.com/bxwhite/javaweb/tree/master/FirstStruts2-master



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