当一个表单提交春季结合例外(Spring binding exception when a form

2019-08-06 03:58发布

卡住,不知道为什么Spring表单是无法成功提交[给绑定的问题]时预填充在GET Request调用loadForm ,但在方法填充时正常工作setupFormObject@ModelAttribute注解标签。 我可以提供在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)
`

你的帮助是高度赞赏

谢谢

Answer 1:

这个问题实际上是UserEntity没有一个默认的构造函数,如果添加的构造,将干净的工作:

public UserEntity(){
    //
}


Answer 2:

我觉得发生了什么事是你的形式在JSP中声明的方式,提交它时,它会假设有一个UserEntity在位置0实例users的列表UserForm实例,但它实际上绊倒空引用。

是的,你做的时候显示的窗体(使用被映射到HTTP GET的loadForm法)一些UserEntity实例添加到模型,但是当你提交表单,创建一个新的模型对象,而这一次将只有空在用户列表中。

尝试使模型会话范围,这样一来提交表单当您创建时所显示的形式模型实例将被重用。 或者创建自己的List实现当执行一个get(INT位置)并没有什么存在,将返回UserEntity的新实例。



文章来源: Spring binding exception when a form is submitted