如何调用在Struts2的一个动作定义了不同的方法呢? [英] How to call different methods defined in one action in struts2?

查看:105
本文介绍了如何调用在Struts2的一个动作定义了不同的方法呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不熟悉Struts2的,但我知道,方法execute()被称为默认动作的名字呼吁行动期间。但如何调用同样的动作类中定义的其他方法?

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?

在下面的例子中的execute()当我在阿贾克斯这样的设置的URL链接方法被称为: saveJSONDataAction.action 感谢@Action注释

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

如何URL应该看起来像由AJAX调用otherMethod()?

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

Action类:

@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调用:

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');
      }
    }
});

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

How to call otherMethod() method by ajax?

推荐答案

有一个名为的 动态方法调用

There is a feature called Dynamic method invocation:

动态方法调用(DMI)将使用字符串下一个!字符中一个动作名称作为一个方法的名称来调用(而不是执行)。一提到范畴!create.action,表示使用类别动作映射,但拨打代替create方法。

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.

请注意,这可能会带来安全漏洞......所以一定要正确地配置这种行为(见公报文档)。

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

这篇关于如何调用在Struts2的一个动作定义了不同的方法呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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