简体   繁体   中英

Passing Array from Jsp to Controller [Spring MVC]

I am trying to pass a Javascript array from JSP to My controller class -

JSP -

var myArray = [];
myArray .push("OU=Software,DC=example,DC=com,");
myArray .push("OU=IT,DC=example,DC=com,");
$("#ADOus").attr("action","${ctx}/ADSetting?myOUsArray ="+ myArray );
$("#ADOus").submit();

Controller -

@RequestMapping(value = { "/ADSetting" },  method=RequestMethod.POST)
    public String configureOUs(HttpServletRequest request,@RequestParam("myOUsArray ") String[] myOUsArray ){
        logger.info("myOUsArray.length "+myOUsArray.length);

        return "";
    }

The problem is length received is 6 rather than 2. I suppose all the comma separated values are been considered as individual values to array. How to resolve this issue, i mean how can the java script array with comma and spaces can be received in my controller class.

You could convert the javascript array to JSON using following code.

JSON.stringify(yourArray);

And in controller, accept it as String parameter and then convert it back to array using Jackson library as below:

ObjectMapper mapper = new ObjectMapper(); String [] array = mapper.readValue(jsonString, String[].class):

Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM