0

I have following ajax

 $.ajax({//jquery ajax
            data:{"images": [1,2,3]},                
            dataType:'json',
            type:"post",
            url:"${prefix}/testarray"
            ....
        });

===============================================
form and method

    public class TestForm {
    private List<String> images=new ArrayList<String>();

    public List<String> getImages() {
        return images;
    }

    public void setImages(List<String> images) {
        this.images = images;
    }  

}

@RequestMapping(value = "/testarray", method = RequestMethod.POST)
public @ResponseBody int testForm(TestForm form){
    return form.getImages().size();
}

and when post data i've got error:

 java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:504)
    at java.lang.Integer.parseInt(Integer.java:527)
    at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:969)
    at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:902)
    at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:75)
    at org.springframework.validation.DataBinder.applyPropertyValues(DataBinder.java:740)
    at org.springframework.validation.DataBinder.doBind(DataBinder.java:636)
    at org.springframework.web.bind.WebDataBinder.doBind(WebDataBinder.java:191)
    at org.springframework.web.bind.ServletRequestDataBinder.bind(ServletRequestDataBinder.java:112)
    at org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.bindRequestParameters(ServletModelAttributeMethodProcessor.java:153)
    at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:106)
    at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:77)
    at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:162)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:123)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:745)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:686)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:920)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:827)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:158)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:801)
    at org.springframework.test.web.servlet.TestDispatcherServlet.service(TestDispatcherServlet.java:66)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:96)
    at org.springframework.mock.web.MockFilterChain$ServletFilterProxy.doFilter(MockFilterChain.java:168)
    at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:136)
    at org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:134)

I know about tradiotanal:true in ajax but how to accept following parameters in spring mvc?:

images[]    1
images[]    2
images[]    3
2
  • I think when you have a ModelAttribute (e.g. TestForm in your example) it is not possible to accept array parameters without setting traditional:true I'd consider this a bug in Spring MVC. Which version are you using? Commented Sep 5, 2014 at 12:52
  • Possible duplicate of Passing in JSON array to spring MVC Controller Commented Mar 23, 2017 at 10:19

1 Answer 1

0

I have gone through your exception stack trace , You are trying to take TestForm object from ajax call while you are passing String array with param name "images".

So follow the below modify code:

@RequestMapping(value = "/testarray", method = RequestMethod.POST)
public @ResponseBody int testForm(String[] images){

    TestForm form=null;
    if(images.length()!=0){
       form=new TestForm();
       form.setImages(Arrays.asList(images));
    }
    return form.getImages().size();
}

And let me know if you are facing problem still

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

3 Comments

About you code it will be work if images.length() to images.length and @RequestParam(value="images[]") String[] images. Thanks for the answer,but about TestForm i'd like get this array of form how to bind images array to field images in testform?
for example i've have object testform with two fields String name and List<String> images and in ajax i send following: data:{"name":"cool","images":[1,2,3]}
Replace your code snippet with the " public @ResponseBody int testForm(@ModelAttribute TestForm form)"

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.