0

我有以下 ajax

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

=================================================
形式和方法

    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();
}

当发布数据时我有错误:

 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)

我知道 tradiotanal:true 在 ajax 但如何在 spring mvc 中接受以下参数?:

images[]    1
images[]    2
images[]    3
4

1 回答 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

于 2013-08-29T09:02:54.337 回答