---
title: "webwork笔记之action"
author: "weishuwei"
date: "2007-06-29T17:32:47Z"
tags: []
slug: "webwork-action"
draft: false
lastmod: "2007-06-29T17:32:47Z"
id: 95585
url: "https://weishuwei.iteye.com/blog/95585"
---

xml 代码

 

01. public interface Action extends Serializable {
02. public static final String SUCCESS = "success";
03. public static final String NONE = "none";
04. public static final String ERROR = "error";
05. public static final String INPUT = "input";
06. public static final String LOGIN = "login";
07. public String execute() throws Exception;
08. }
09. =========================================================================
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. &lt;action name="test" class="com.javaeye.action.TestAction"&gt;&lt;/action&gt;
43. &lt;action name="login" class="com.javaeye.action.TestAction" method="login"&gt;&lt;/action&gt;
44. =========================================================================
45. 有时我们只是页面跳转,并不需要声明一个action,可以使用ActionSupport
46. &lt;action name="index" class="com.opensymphony.xwork.ActionSupport"&gt;
47. &lt;result name="success" type="dispatcher"&gt;/index.jsp&lt;/result&gt;
48. &lt;/action&gt;
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. &lt;action name="listUser" class="com.adt.action.user.ListUser"&gt;
60. &lt;param name="page.everyPage"&gt;10&lt;/param&gt;
61. &lt;result name="success"&gt;/user/user\_list.jsp&lt;/result&gt;
62. &lt;/action&gt;
63. =========================================================================