java 如何在struts2中调用一个动作中定义的不同方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16872769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 00:13:29  来源:igfitidea点击:

How to call different methods defined in one action in struts2?

javaajaxstruts2

提问by Roman

I'm not familiar with struts2, but I know that method execute() is called as default in Action during call to action by name. But how to call other method defined in the same action class?

我不熟悉 struts2,但我知道方法 execute() 在按名称调用操作期间在 Action 中作为默认调用。但是如何调用同一个动作类中定义的其他方法呢?

In an example below execute() method is called when I set url link in ajax like that: saveJSONDataAction.actionthanks to @Action annotation.

在下面的示例中,当我在 ajax 中设置 url 链接时调用 execute() 方法:saveJSONDataAction.action感谢@Action 注释。

How the url should looks like to call otherMethod() by ajax?

url 应该如何通过 ajax 调用 otherMethod()?

Action class:

动作类:

@ParentPackage("json-default")
@Action(value="getJSONDataAction")
@Result(name="success", type="json")
public class JSONDataAction extends ActionSupport {
    private static final long serialVersionUID = 1L;

    private List<Report> data = new ArrayList<Report>();

    public JSONDataAction(){
        data.add(new Report(1, "Chris", true, "2008-01-01", "orange"));
    }

    public String execute() {
        return SUCCESS;
    }

    public String otherMethod() {
        //do something else ..
        return SUCCESS;
    }

    // getters and setters
}

Ajax call:

阿贾克斯调用:

$.ajax({
    url: "../json/saveJSONDataAction.action",
    data: data,
    dataType: 'json',
    contentType: 'application/json',
    type: 'POST',
    success: function (res) {
      if (res.result === 'ok') {
        $console.text('Data saved');
      }
    }
});

How to call otherMethod() method by ajax?

如何通过ajax调用otherMethod()方法?

回答by Armaggedon

You can specify what method is executed in struts.xmlfile. You just need to override the default, which is execute.

您可以指定在struts.xml文件中执行的方法。您只需要覆盖默认值,即execute.

<action name="MyMainAction" class="foo.bar.MyAction">
     <result>result.jsp</result>
</action>
<!-- This will call execute() -->

<action name="MySecondAction" class="foo.bar.MyAction" method="secondExecute">
     <result>result.jsp</result>
</action>
<!-- This will call secondExecute() -->

Then, you just need to call ../json/MySecondAction.actionin your function.

然后,您只需要调用../json/MySecondAction.action您的函数。

回答by Pavel Horal

There is a feature called Dynamic method invocation:

有一个称为动态方法调用的功能:

Dynamic Method Invocation (DMI) will use the string following a "!" character in an action name as the name of a method to invoke (instead of execute). A reference to "Category!create.action", says to use the "Category" action mapping, but call the create method instead.

动态方法调用 (DMI) 将使用“!”后面的字符串 操作名称中的字符作为要调用(而不是执行)的方法的名称。对“Category!create.action”的引用表示使用“Category”动作映射,而是调用 create 方法。

Note, that it might introduce security hole... so be sure to configure this behavior properly (see the oficial documentation).

请注意,它可能会引入安全漏洞...因此请务必正确配置此行为(请参阅官方文档)。