如何在Spring中传递多个请求参数?

时间:2022-02-08 16:41:51

In my Spring Application i'm pass two request parameters to my business logic..

在Spring应用程序中,我向业务逻辑传递两个请求参数。

$.ajax({
        url : "classes/addResult",
        type:'POST',
        data : {"names":names,"globalClassId":globalClassId}

    });

And my business logic

我的业务逻辑

@RequestMapping(value = "addResult", method = RequestMethod.POST)
public String addResult(ResultForm form,
        BindingResult result, Model model,
        @RequestParam("names") String[] names,
        @RequestParam("globalClassId") String globalClassId)
        throws Exception {
        -------------
        ------------
    return "";
}

But controller not calling to this method .. Why is their is any wrong my code..

但是控制器不调用这个方法。为什么他们的代码有错误

2 个解决方案

#1


2  

<script type="text/javascript">
   var names = new Array();
    $.ajax({
        url : "Result",
        type : 'POST',
        data : {
            "names" : JSON.stringify(names),//or names.join()
            "globalClassId" : globalClassId
        }});
</script>

#2


1  

If you are getting error 400 (Bad request) ????????????

如果您收到错误400(错误请求)??????????

when you pass array data ({names:names} in your case ) to $.ajax() method then it append squre brackets [] after the paremeter name (means paremeter names will be names[] //not names)

当您将数组数据(在您的例子中是{name:names})传递给$.ajax()方法时,它会在paremeter名称之后附加squre括号[](意思是paremeter名称将是names[] /不是name)

therefor you need some changes in your code

因此,您需要对代码进行一些更改

@RequestMapping(value = "addResult", method = RequestMethod.POST)
public String addResult(ResultForm form,
        BindingResult result, Model model,
        @RequestParam("names[]") String[] names, //replace names with names[]
        @RequestParam("globalClassId") String globalClassId)
        throws Exception {
        -------------
        ------------
    return "";
}

or you can use

或者您也可以使用

@RequestMapping(value = "addResult", method = RequestMethod.POST)
    public String addResult(ResultForm form,
            BindingResult result, Model model,
            @RequestParam("globalClassId") String globalClassId)
            throws Exception {
        String[] names = request.getParameterValues("names[]");//getting names array here
            -------------
            ------------
        return "";
    }

#1


2  

<script type="text/javascript">
   var names = new Array();
    $.ajax({
        url : "Result",
        type : 'POST',
        data : {
            "names" : JSON.stringify(names),//or names.join()
            "globalClassId" : globalClassId
        }});
</script>

#2


1  

If you are getting error 400 (Bad request) ????????????

如果您收到错误400(错误请求)??????????

when you pass array data ({names:names} in your case ) to $.ajax() method then it append squre brackets [] after the paremeter name (means paremeter names will be names[] //not names)

当您将数组数据(在您的例子中是{name:names})传递给$.ajax()方法时,它会在paremeter名称之后附加squre括号[](意思是paremeter名称将是names[] /不是name)

therefor you need some changes in your code

因此,您需要对代码进行一些更改

@RequestMapping(value = "addResult", method = RequestMethod.POST)
public String addResult(ResultForm form,
        BindingResult result, Model model,
        @RequestParam("names[]") String[] names, //replace names with names[]
        @RequestParam("globalClassId") String globalClassId)
        throws Exception {
        -------------
        ------------
    return "";
}

or you can use

或者您也可以使用

@RequestMapping(value = "addResult", method = RequestMethod.POST)
    public String addResult(ResultForm form,
            BindingResult result, Model model,
            @RequestParam("globalClassId") String globalClassId)
            throws Exception {
        String[] names = request.getParameterValues("names[]");//getting names array here
            -------------
            ------------
        return "";
    }