使用不同的参数调用 Struts 2 操作方法 [英] invoke Struts 2 action method with different arguments

查看:40
本文介绍了使用不同的参数调用 Struts 2 操作方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 struts 2 actionclass 中,我有一个方法:

In my struts 2 actionclass, I have a method:

public String doXXX(String param){

//do stuff
return SUCCESS;
}

在我的配置 xml 中,如何根据操作使用不同的 'param' 值调用此方法.类似的东西:

In my config xml, how can I call this method with different values for 'param' based on the action. Something like:

<action name="action1" class="struts2Class" method="doXXX" param = "foo" />
<action name="action2" class="struts2Class" method="doXXX" param = "bar" />

推荐答案

您不能在要用作操作方法的方法中传递参数.您可以在 struts.xml 中为您的操作指定一个 param 元素,如下所示:

You can't pass arguments in the method which you intend to use as the action method. You can specify a param element in the struts.xml for your action as follows:

<action name="action1" class="struts2Class" method="doXXX">
  <param name="foo">bar</param>
</action>

然后,您需要使用 getter 和 setter 在 struts2Class 上声明一个名为 foo 的私有变量.调用 action1 时将设置此属性.

You then need to declare a private variable called foo on your struts2Class with getters and setters. This property will be set when action1 is invoked.

属性 foo 将作为动作执行的一部分由参数拦截器设置.

The property foo will be set by the Param Interceptor as part of the action execution.

这篇关于使用不同的参数调用 Struts 2 操作方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆