通过Ajax调用HTTP参数数组使用Struts 2 [英] HTTP Array Parameters with Struts 2 via an Ajax Call

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

问题描述

我有一个问题,发送数组参数一个Struts 2 Action类。我用struts 2.1.8.1。

I'm having an issue sending array parameters to a Struts 2 action class. I am using struts 2.1.8.1.

下面是一些例子code:

Here is some example code:

public class MyAction extends ActionSupport {

    private String[] types;

    public String execute() {
        return SUCCESS;
    }

    public String[] getTypes() {
        return types;
    }

    public void setTypes(String[] types) {
        this.types = types;
    }
}

通过jQuery的AJAX方法发送一个阵列时,现在的问题是:

The problem is when sending an array via the jquery ajax method:

$.ajax({
    type: 'POST',
    url: 'Myaction.action',
    data: {
        types: ["this", "is", "a", "test"]
    }
});

导致异常发生:

causes an exception to occur:

ognl.ParseException:遇到]],第1行,第7列

我如何使用jQuery数组发送给我的Struts2的action类?有没有沿着我需要包括一个拦截器的线什么?还是有jQuery中的选项删除呢?

How can I use jQuery to send the array to my Struts2 action class? Is there something along the lines of an interceptor that I need to include? Or is there an option in jQuery to remove this?

我也遇到了这个问题与jQuery UI的可排序控制,但我解决了使用正则表达式来删除[]字符。我想避免这种情况,因为该解决方案困扰我。我想我可以只建立,而不是使用对象符号的字符串自己,但除非你能说服我,否则,我想用对象符号来代替。

I also encountered this problem with the jQuery UI Sortable control, but I solved that using a regex to remove the "[]" characters. I would like to avoid that, because that solution bothers me. I suppose I could just build the string myself, instead of using the object notation, but unless you can convince me otherwise, I would like to use the object notation instead.

推荐答案

IIRC的Struts不喜欢了jQuery 1.4 +格式,可以使用传统的格式虽然,只是把这个任何时候您的 $阿贾克斯() 拨打:

IIRC Struts doesn't like the jQuery 1.4+ format, you can use the traditional format though, just put this any time before your $.ajax() call:

$.ajaxSettings.traditional = true; 

您可以阅读更多有关1.4+默认VS传统的序列化在 $。参数()文档,最好的例证就是他们简单的例子:

You can read more about the 1.4+ default vs traditional serialization in the $.param() documentation, the best illustration is their short example:

// <=1.3.2: (traditional in 1.4+)
$.param({ a: [2,3,4] }) // "a=2&a=3&a=4"
// >=1.4: (default in 1.4+)
$.param({ a: [2,3,4] }) // "a[]=2&a[]=3&a[]=4"

这篇关于通过Ajax调用HTTP参数数组使用Struts 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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