struts2的ActionProxy


ctinoProxy的全名是com.opensymphony.xwork2.ActionProxy,

ActionProxy = Action + Proxy,從字面意思來解釋:Action的代理。

 

在Struts中,ActionContext、ActionInvocation、ActionProxy、ActionConfig聯系的很緊密。

1
ActionContext<-->ActionInvocation<-->ActionProxy-->ActionConfig

(1.1)從ActionContext對象可以得到ActionInvocation對象

(1.2)從ActionInvocation對象可以得到ActionContext對象

(2.1)從ActionInvocation對象可以得到ActionProxy對象

(2.2)從ActionProxy對象可以得到ActionInvocation對象

(3.1)從ActionProxy對象可以得到ActionConfig對象

 

在搜索的時候 ,遇到一句描述ActionInvocation和ActionProxy關系的話,感覺非常切中要害:

Essentially, ActionProxy encapsulates how an Action can be obtained. ActionInvocation encapsulates how the Action is executed when a request is invoked.

本質上來,ActionProxy對如何獲取Action對象進行了封裝,而ActionInvocation對如何執行Action進行了封裝。

 

原話地址:https://struts.apache.org/docs/action-proxy-actionproxy-factory.html

 

Typically, an ActionProxy will utilize the ActionInvocation to encapsulate the execution of a particular request.

The ActionInvocation determines how an Action is handled: Is it being intercepted? Is there a PreResultListener acting on it?

 

wKioL1dz40yDz_qjAABmO-FTeq4881.png

在上面圖中,

ActionConfig,負責從struts.xml文件中讀取配置;

ActionSupport,我們自己實現的Action類一般都繼承ActionSupport類;

ActionContext,是Action運行的環境,為Action類提供一些信息;

ActionProxy,是如何獲取Action;

ActionInvocation是如何執行Action。

 

上面標的(1)至(4),都能通過ActionProxy來獲得:

(1)表示當前Action類在struts.xml文件中對應的<action>標簽所在的<package>標簽的namespace屬性

(2)表示<action>標簽的name屬性

(3)表示真實的Action類的實例,不是字符串類型,而<action>標簽的class屬性對應的類的實例化。

(4)<action>標簽的method屬性

 

從一些java培訓機構的視頻來看,ActionProxy實現了URL和真正Action類之間的映射。

 


 

1、如何獲取ActionProxy對象

1
ActionProxy proxy = invocation.getProxy();

 

 

2、如何獲取ActionProxy中的值

(1)獲取當前<action>所在的命名空間

1
String namespace = proxy.getNamespace();

 

(2)獲取<action>的名字

1
String actionName = proxy.getActionName();

 

(3)獲取Action類的實例

1
Object action = proxy.getAction();

 

(4)獲取<action>執行的方法

1
String method = proxy.getMethod();

 

(5)得到ActionInvocation和ActionConfig對象

1
2
3
ActionConfig config = proxy.getConfig();
 
ActionInvocation invocation2 = proxy.getInvocation();

 

 

 

3、ActionProxy的源代碼

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package  com.opensymphony.xwork2;
 
import  com.opensymphony.xwork2.config.entities.ActionConfig;
 
 
/**
  * ActionProxy is an extra layer between XWork and the action so that different proxies are possible.
  * <p/>
  * An example of this would be a remote proxy, where the layer between XWork and the action might be RMI or SOAP.
  *
  * @author Jason Carreira
  */
public  interface  ActionProxy {
 
     /**
      * Gets the Action instance for this Proxy.
      *
      * @return the Action instance
      */
     Object getAction();
 
     /**
      * Gets the alias name this ActionProxy is mapped to.
      *
      * @return the alias name
      */
     String getActionName();
 
     /**
      * Gets the ActionConfig this ActionProxy is built from.
      *
      * @return the ActionConfig
      */
     ActionConfig getConfig();
 
     /**
      * Sets whether this ActionProxy should also execute the Result after executing the Action.
      *
      * @param executeResult <tt>true</tt> to also execute the Result.
      */
     void  setExecuteResult( boolean  executeResult);
 
     /**
      * Gets the status of whether the ActionProxy is set to execute the Result after the Action is executed.
      *
      * @return the status
      */
     boolean  getExecuteResult();
 
     /**
      * Gets the ActionInvocation associated with this ActionProxy.
      *
      * @return the ActionInvocation
      */
     ActionInvocation getInvocation();
 
     /**
      * Gets the namespace the ActionConfig for this ActionProxy is mapped to.
      *
      * @return the namespace
      */
     String getNamespace();
 
     /**
      * Execute this ActionProxy. This will set the ActionContext from the ActionInvocation into the ActionContext
      * ThreadLocal before invoking the ActionInvocation, then set the old ActionContext back into the ThreadLocal.
      *
      * @return the result code returned from executing the ActionInvocation
      * @throws Exception can be thrown.
      * @see ActionInvocation
      */
     String execute()  throws  Exception;
 
     /**
      * Gets the method name to execute, or <tt>null</tt> if no method has been specified (meaning <code>execute</code> will be invoked).
      *
      * @return the method to execute
      */
     String getMethod();
 
     /**
      * Gets status of the method value's initialization.
      *
      * @return true if the method returned by getMethod() is not a default initializer value.
      */
     boolean  isMethodSpecified();
     


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM