使用ajax将数组数据从浏览器中的javascript传递到spring mvc控制器 [英] Pass array data from javascript in browser to spring mvc controller using ajax

查看:49
本文介绍了使用ajax将数组数据从浏览器中的javascript传递到spring mvc控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 AJAX 将 Web 浏览器中的 javascript 数组传递给 Spring MVC 控制器

I would like to pass an array from javascript in web browser to a Spring MVC controller using AJAX

在javascript中,我有

In javascript, I have

var a = [];
a[0] = 1;
a[1] = 2;
a[2] = 3;

// how about multiple arrays as well?

$.ajax({
    type : "POST",
    url : "/myurl",
    data : //not sure how to write this, ("a="+a), ?
    success : function(response) {
       // do something ... 
    },
    error : function(e) {
       alert('Error: ' + e);
    }
}); 

在Java中,我想创建一个类来接收来自AJAX的数据,我创建了一个类来接收数据

In Java, I would like to create a class to receive data from AJAX, and I create a class to receive data

package com.amazon.infratool.ui;

import lombok.Getter;
import lombok.Setter;


@Setter @Getter
public class RepairInfomationParameters {
//how to write this variable?
    List<String> a = null; // is it something like this?
}

这样做的正确方法是什么?谢谢!

What is the correct way to do this? Thanks!

推荐答案

您可以从 JavaScript 端执行此操作:

You can do this from the JavaScript side:

$.ajax({
    type : "POST",
    url : "/myurl",
    data : {
        myArray: a //notice that "myArray" matches the value for @RequestParam
                   //on the Java side
    },
    success : function(response) {
       // do something ... 
    },
    error : function(e) {
       alert('Error: ' + e);
    }
}); 

然后在Java端(在Spring 3中),假设这个方法被/myurl映射:

Then on the Java side (in Spring 3), assuming that this method is mapped by /myurl:

public String controllerMethod(@RequestParam(value="myArray[]") Integer[] myArray){
    ....
}

我相信以下方法也适用:

I believe the following will also work:

public String controllerMethod(@RequestParam(value="myArray[]") List<Integer> myArray){
    ....
}

Spring 足够聪明,可以弄清楚如何进行绑定.

Spring is smart enough to figure out how to do the binding.

对于多个数组,您可能只想拥有一个命令对象:

For multiple arrays, you might want to just have a command object:

public class MyData {
    private List<Integer> firstArray;
    private List<Integer> secondArray;
    private List<Integer> thirdArray;

    ...
    ...
}

然后在 JavaScript 方面:

Then on the JavaScript side:

$.ajax({
    type : "POST",
    url : "/myurl",
    data : {            
        myData: {
           "firstArray": firstArray,
           "secondArray": secondArray,
           "thirdArray": thirdArray
        }            
    },
    success : function(response) {
       // do something ... 
    },
    error : function(e) {
       alert('Error: ' + e);
    }
}); 

在 Java 端,您可以使用 @ModelAttribute 进行绑定:

On the Java side, you can bind using @ModelAttribute:

public String controllerMethod(@ModelAttribute(value="myData") MyData myData) throws ParseException {
    ....
}

编辑

@RequestParam 注释更改为使用 myArray[] 而不仅仅是 myArray,因为此更改似乎是在 Spring 之后进行的此答案首次发布.

Changed the @RequestParam annotation to use myArray[] instead of just myArray, since this change appears to have been made in Spring after this answer was first posted.

这篇关于使用ajax将数组数据从浏览器中的javascript传递到spring mvc控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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