Struts ActionProxy:如何设置方法?

时间:2013-04-04 18:28:52

标签: java struts2 junit struts opensymphony

我正在使用openSymphony 2.1.2,并且我有一些为旧版本的openSymphony编写的旧代码。

正如您所看到的,它告诉我ActionProxy没有方法setMethod(String)。 它可能是used to have。但是not anymore

但它仍然有方法String getMethod()。那么我怎么能得到一个方法变量,但是没有设置?如何在新API中设置方法?

import com.opensymphony.xwork2.ActionProxy;

protected ActionProxy proxy = null;

//used to work with older versions of openSymphony, now there's a compilation error here:
//The method setMethod(String) is undefined for the type ActionProxy
proxy.setMethod(methodName);

更新

我正在研究一个使用openSymphony 2.1的系统(我不是我写的)。和Struts 2.3.8。

在该系统上,我正在尝试应用以下代码:

Action Unit Testing with Struts 2.0. Part 2

该代码因我的项目使用的openSymphony和Struts版本而失败。所以我试图将该代码升级到较新版本的openSymphony和Struts。

我需要它,以便能够在基于不同框架的系统中模拟从我的系统运行struts操作。

2 个答案:

答案 0 :(得分:1)

DefaultActionProxy正在实施ActionProxy。要设置method属性,您应该扩展DefaultActionProxy并通过setMethod实施设置超级保护属性。

然后你应该DefaultActionProxyFactory扩展DefaultActionProxy并替换createActionProxy()中的struts.xml

之后修改<bean type="com.opensymphony.xwork2.ActionProxyFactory" name="xwork" ... /> 并覆盖

method:

拥有自己的工厂。

但最好是使用setMethod前缀在参数中设置方法,或者使用ActionMapping类中的ActionContext设置DefaultActionMapper,或者可以延长{{1}}获取您需要的映射。请参阅docs

答案 1 :(得分:0)

只需使用createActionProxy的{​​{1}}方法,其ActionProxyFactory作为其中一个参数。

methodName

public ActionProxy createActionProxy(String namespace, String actionName,
    String methodName, Map<String, Object> extraContext, boolean executeResult,
    boolean cleanupContext);

public ActionProxy createActionProxy(String namespace, String actionName,
    String methodName, Map<String, Object> extraContext);

请参阅:http://svn.apache.org/viewvc/struts/struts2/tags/STRUTS_2_3_8/xwork-core/src/main/java/com/opensymphony/xwork2/ActionProxyFactory.java?view=markup