webwork筆記之action

xml 代碼
 
  1. public interface Action extends Serializable {   
  2.     public static final String SUCCESS = "success";  
  3.     public static final String NONE = "none";  
  4.     public static final String ERROR = "error";  
  5.     public static final String INPUT = "input";  
  6.     public static final String LOGIN = "login";  
  7.     public String execute() throws Exception;  
  8. }  
  9. =========================================================================  
  10. 2,每次Action調用都會創建一個AcionContext,我們可以ActionContext context=ActionContext.getContext()來使用它.  
  11. =========================================================================  
  12. 獲取Session  
  13. 方式一:  
  14. 以下語句必須放在execute()方法裏,不然會取不到值;  
  15. ActionContext ctx2 = ActionContext.getContext();//必須在execute()方法裏  
  16. Map session2=ctx2.getSession();  
  17. 方式二:  
  18. action implements SessionAware{  
  19. protected Map session;  
  20. public void setSession(Map session) {  
  21.     this.session = session;  
  22. }  
  23. }  
  24. 這樣就可以從該session對象中put或get對象.  
  25. 方式三:  
  26. 前兩個得到是session都不是HttpSession對象.而ServletActionContext.getRequest().getSession()則是HttpSession對象.  
  27. ServletActionContext.getRequest().getParameter("username")  
  28. ServletActionContext.getRequest()就是HttpServletRequest  
  29. =========================================================================  
  30. webwork中的aware接口   
  31. aware接口要求實現類必須實現單一的setter方法,以便將資源應用到實現類中。  
  32. 1、ParametersAware 指明action需要HTTP請求參數map。  
  33. 2、ServletRequestAware 指明action需要ServletRequest對象。  
  34. 3、SessionAware 指明action需要HttpSession對象。  
  35. 4、ApplicationAware 指明action需要ServletContext對象。   
  36. =========================================================================  
  37. 可以處理多個請求的Action  
  38. "!method"的方式支持多個請求,當我們用/test.action時,則調用execute方法,我們用/test!login.action時,將調用login方法,  
  39. 即 actionName!method.action 這樣的URL映射方式。此外login()方法也可以命名爲 doLogin() 方法.  
  40. =========================================================================  
  41. 驗證文件採用ActionClass-validation.xml的命名規則,並不支持分別針對不同的method單獨定義驗證文件,我們可以採用如下方式:  
  42. <action name="test" class="com.javaeye.action.TestAction"></action>  
  43. <action name="login" class="com.javaeye.action.TestAction" method="login"></action>  
  44. =========================================================================  
  45. 有時我們只是頁面跳轉,並不需要聲明一個action,可以使用ActionSupport  
  46. <action name="index" class="com.opensymphony.xwork.ActionSupport">  
  47.     <result name="success" type="dispatcher">/index.jsp</result>  
  48. </action>  
  49. =========================================================================  
  50. 異常可以如下方式獲取:  
  51. OgnlValueStack s = ActionContext.getContext().getValueStack();  
  52. Object obj = s.pop();  
  53. System.out.println(obj.getClass().getName());  
  54. obj = s.pop();  
  55. System.out.println(obj.getClass().getName());  
  56. BTW:如果是以chain的方式轉向於此action,則要注意第二個元素纔是ExceptionHolder   
  57. =========================================================================  
  58. 爲action參數注入值;  
  59. <action name="listUser" class="com.adt.action.user.ListUser">    
  60.       <param name="page.everyPage">10</param>    
  61.       <result name="success">/user/user_list.jsp</result>    
  62. </action>  
  63. =========================================================================  
發佈了2 篇原創文章 · 獲贊 1 · 訪問量 4133
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章