提交表单时的Spring绑定异常

时间:2013-01-09 11:47:17

标签: spring spring-mvc

在获取Request调用loadForm时预先填充弹出表单无法成功提交[给出绑定问题]的原因并且无法理解,但在方法setupFormObject中填充时效果正常带有@ModelAttribute注释标记的1}}。我可以在github中提供一个简单的例子来测试是否要求:)

以下示例

@ModelAttribute("showForm")
public ShowForm setupFormObject() {
    //Instantiate showForm with data
    return showForm;
}

@RequestMapping(method = RequestMethod.GET)
public ModelAndView loadForm(@RequestParam("id") String id, HttpSession session) {    
    ModelAndView modelAndView = new ModelAndView(nextPage);
    //Instantiate showForm with data
    //modelAndView.addObject("showForm", showForm);
    return modelAndView;
}

@RequestMapping(method = RequestMethod.POST)
public String post(@ModelAttribute("showForm") ShowForm showForm, BindingResult result, final RedirectAttributes redirectAttrs) {
     //I see changed data here in showForm when populated using @setupFormObject
     //See an exception in JSP with binding error if populated in loadForm
     return "";
 }

按要求堆栈跟踪。此例外来自github示例。

`HTTP Status 500 - Request processing failed; nested exception is org.springframework.beans.InvalidPropertyException: Invalid property 'users[0]' of bean class [com.example.UserForm]: Illegal attempt to get property 'users' threw exception; nested exception is org.springframework.beans.NullValueInNestedPathException: Invalid property 'users' of bean class [com.example.UserForm]: Could not instantiate property type [com.example.UserEntity] to auto-grow nested property path: java.lang.InstantiationException: com.example.UserEntity`

`type Exception report`

`message Request processing failed; nested exception is org.springframework.beans.InvalidPropertyException: Invalid property 'users[0]' of bean class [com.example.UserForm]: Illegal attempt to get property 'users' threw exception; nested exception is org.springframework.beans.NullValueInNestedPathException: Invalid property 'users' of bean class [com.example.UserForm]: Could not instantiate property type [com.example.UserEntity] to auto-grow nested property path: java.lang.InstantiationException: com.example.UserEntity`

`description The server encountered an internal error that prevented it from fulfilling this request.`

`exception`

`org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.beans.InvalidPropertyException: Invalid property 'users[0]' of bean class [com.example.UserForm]: Illegal attempt to get property 'users' threw exception; nested exception is org.springframework.beans.NullValueInNestedPathException: Invalid property 'users' of bean class [com.example.UserForm]: Could not instantiate property type [com.example.UserEntity] to auto-grow nested property path: java.lang.InstantiationException: com.example.UserEntity`
    `org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:927)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:822)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)`

`root cause`

`org.springframework.beans.InvalidPropertyException: Invalid property 'users[0]' of bean class [com.example.UserForm]: Illegal attempt to get property 'users' threw exception; nested exception is org.springframework.beans.NullValueInNestedPathException: Invalid property 'users' of bean class [com.example.UserForm]: Could not instantiate property type [com.example.UserEntity] to auto-grow nested property path: java.lang.InstantiationException: com.example.UserEntity
    org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:829)
    org.springframework.beans.BeanWrapperImpl.getNestedBeanWrapper(BeanWrapperImpl.java:556)
    org.springframework.beans.BeanWrapperImpl.getBeanWrapperForPropertyPath(BeanWrapperImpl.java:533)
    org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:894)
    org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:75)
    org.springframework.validation.DataBinder.applyPropertyValues(DataBinder.java:699)
    org.springframework.validation.DataBinder.doBind(DataBinder.java:595)
    org.springframework.web.bind.WebDataBinder.doBind(WebDataBinder.java:191)
    org.springframework.web.bind.ServletRequestDataBinder.bind(ServletRequestDataBinder.java:112)
    org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.bindRequestParameters(ServletModelAttributeMethodProcessor.java:153)
    org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:106)
    org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:77)
    org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:162)
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:123)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:746)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:687)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:915)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:822)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
`

非常感谢您的帮助

由于

2 个答案:

答案 0 :(得分:30)

问题实际上是UserEntity没有默认构造函数,如果你添加构造函数它会干净地工作:

public UserEntity(){
    //
}

答案 1 :(得分:0)

我认为正在发生的事情是你在JSP中声明表单的方式,在提交表单时,它会假设在UserEntity users列表中的{0}个实例位置UserForm 1}}实例,但它实际上偶然发现了一个空引用。

是的,在显示表单时(使用映射到HTTP Get的loadForm方法),您会向模型添加一些UserEntity实例,但是当您提交表单时,会创建一个新的模型对象,只有用户列表中的空值。

尝试使模型会话作用域,这样在表单显示时创建的模型实例将在提交表单时重用。或者创建自己的List实现,当执行get(int position)并且没有任何内容时,它将返回UserEntity的新实例。