Springmvc to receive multiple checkbox submission Parameters __springmvc

Source: Internet
Author: User

Springmvc the problem when receiving multiple CheckBox submission Form parameters ...

This is a problem when the current-end form is serialized as a JSON object submitting the checkbox data.


As shown in figure I have 3 checkbox data, checkbox's name is position, when the check is checked one time SPRINGMVC receives the parameter the name is position, when submits more than one time receives the front end the parameter to need the array, This is the time to use the following code on the Springmvc method

@RequestMapping ("/add.html")
    @ResponseBody public
    resultmodel Add (httpservletrequest request, @ Requestparam (value = "position[]", required = False) string[] positions) {
        map map = getparammap (request);
        Map.put ("positions", positions);
        return boardservice.addboardcategory (map);
    
There's a problem. CheckBox tick pick a time to receive name is position, more than one time is position[], so the back end is not to make a second judgment, so very troublesome, so very want to solve this problem, At this point, you need to change the name of the checkbox on the front end, as follows

<span class= "PC" >
      <input type= "checkbox" Name= "position[" "value=" 1 "/> Popular classic
      <input type=" CheckBox "Name=" position[] "value=" 2 "/> have good taste creative
      <input type=" checkbox "Name=" position["value=" 3 "/> Shopping home Good shop on
</span>
<span style= "Display:none" class= "app" >
      <input type= "checkbox" Name= " Position[] "value=" 4 "/> Purchasing home
      <input type=" checkbox "Name=" position["" value= "5"/> have good goods home
</span >
This modified name name can also be received by position[when submitting a single parameter receive so that the backend does not have to make redundant judgments.

The front end code is attached below

 $ ("#addOrUpdateBt"). Click (function () {var CategoryName = $ ("#boardCategoryForm #categoryName"). Val ();
            if ($.trim (categoryname). length = = 0) {alert ("Please fill in the main title");
        Return
        var listimghref = $ ("#boardCategoryForm #listImgHref"). Val ();
            if ($.trim (listimghref). length = = 0) {alert ("Please upload list main diagram");
        Return
        var id = $ ("#boardCategoryForm #id"). Val ();
        var url = "";
        if ($.trim (ID). length!= 0) {url = "${ctx}/admin/boardcategory/operation/update.html";
        else {url = "${ctx}/admin/boardcategory/operation/add.html"; } $.ajax ({type: Post, Url:url, data: $ ("#boardCategoryForm"). Serializeobj
                    ECT (), DataType: "JSON", success:function (Result) {if (result.state = 0) {
                    $ ("#boardCategoryDiv"). Modal ("Hide"); $ ("#boardCategorytable "). Bootstraptable (" refresh ");
                else {alert (result.msg);
    }
            }
        }); });

Front-end forms submit parameters by SerializeObject

SPRINGMVC receive array parameters when there are some problems, reported 400 errors, Google the next SPRINGMVC receive an array of related examples, combined with some documents to solve the problem. When there is no checkbox to submit the parameters, the SPRINGMVC receive the parameters of the place required changed to false, to solve the problem of 400, the following is a Google out of the SPRINGMVC example

--------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------

If JQuery is going to pass an array parameter to the server, the requested method is as follows

$.ajax ({
    URL: ${yoururl},
    data: {yourparam:[1,2,3,4]},
    success:function (data) {
    }}
);

Or:

$.ajax ({
    URL: ${yoururl},
    data: {"yourparam[]": [1,2,3,4]},
    success:function (data) {
    }
});

The front-end pass parameter can be written in two ways, it is suggested to write the second, and the receiving parameter of the server is corresponding.

So how does the controller at the service end receive this parameter.

@RequestMapping ("/save") public
Void Save (@RequestParam (value= "yourparam[]" string[] yourparam) {
    //Do Something ...
}

Note: @RequestParam must be in the form of an array of "yourparam[]" as the receiving parameter, so that the correct reception of the front end of the array, if the front end of the array is empty, received is also empty string. For example: The parameters passed by the front end: data:{"yourparam[]": [Null,null,null]} then the parameters received by the server will be ["", "", ""] no need to worry about the null pointer problem.

If you do not use "yourparam[" as the receiving parameters, there will be inexplicable errors.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.