Pass a multidimensional array from jsp to controll

2019-09-18 01:41发布

问题:

I am creating an update page where I am using jquery to create an 2 dimensional array which consists of updated propertyName and the old value. I am doing this so that I do not need to check on server for updated properties. This updatedParam variable I need to pass it to Spring controller. How should this be done? This variable is not part of the model backed form.

I have tried the following in the controller, but it does not work

@RequestMapping(value = "/updateEmployee.html", method = RequestMethod.POST)
public String updateEmployee(                       
        BindingResult result, @ModelAttribute("employee") Employee employee,
        ModelMap model,
        @ModelAttribute(value = "updatedParam") Map<String,String> updatedParam) {

I even tried this but even it does not work

@RequestMapping(value = "/updateEmployee.html", method = RequestMethod.POST)
public String updateEmployee(                       
        BindingResult result, @ModelAttribute("employee") Employee employee,
        ModelMap model,
        @ModelAttribute(value = "updatedParam") String[][] updatedParam) {

NOTE: updatedParam is the variable which I generated in my jsp and which I wish to pass when I call this action on form submission.

Also please note the form submission code

$("#updateEmployeeForm").submit(function(){
    //Compute updatedParam Here
    // I tried submiting the form using ajax and passing updated param as data but does not work 

});

Please advise. In firebug console I keep getting the following error message but I do not think its related to this

The character encoding of the HTML document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the page must be declared in the document or in the transfer protocol.

回答1:

I am doing this so that I do not need to check on server for updated properties.

This is wrong.

You should always check on server everything that is send by user.
Client side validation is not sufficient, as client can easily fool it.

What I'd recommend is simply to make propertyName a part of form backing object and validate it server side.