0

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.

1 Answer 1

2

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!

Sign up to request clarification or add additional context in comments.

1 Comment

Solution is working but is there way around with wrapper classes?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.