spring mvc error将json数组发送到控制器

时间:2013-08-29 07:20:21

标签: jquery json spring spring-mvc

我关注了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:在ajax中是真的,但是如何在spring mvc中接受以下参数?:

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

1 个答案:

答案 0 :(得分:0)

我已经完成了你的异常堆栈跟踪, 您正在尝试从ajax调用中获取TestForm对象,同时传递带有参数名称为“images”的String数组。

请按照以下修改代码:

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

如果您还面临问题,请告诉我