Printemps de liaison exception lorsqu'un formulaire est envoyé
Coincé et n'a aucune idée du pourquoi du Printemps Formulaire n'est pas en mesure de présenter avec succès [donne liaison problème] lors de la pré-rempli en obtenir Request
appel loadForm
, mais qui fonctionne bien quand peuplée dans une méthode setupFormObject
avec @ModelAttribute
du tag. Je peux fournir un exemple simple dans github, pour tester si on me le demande 🙂
Exemple ci-dessous
@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 "";
}
Trace de la pile comme demandé. Cette exception est de github exemple.
`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)
`
Votre aide est très appréciée
Grâce
Je pense que plus d'information est nécessaire ici @java_dude, il sera grand si vous pouvez fournir un petit projet qui reproduit le comportement
Voici le lien vers le projet qui reproduit le comportement de github.com/hth/StatusInvoke . Regardez l' / src / com / example / UserController.java. Si vous avez des questions concernant le projet, faites-le moi savoir. Merci.
Avez-vous été en mesure d'essayer de l'exemple?
post le JSP stacktrace s'il vous plaît 🙂
Ajouté stacktrace exception. Exception est à partir de github exemple. HTH
Voici le lien vers le projet qui reproduit le comportement de github.com/hth/StatusInvoke . Regardez l' / src / com / example / UserController.java. Si vous avez des questions concernant le projet, faites-le moi savoir. Merci.
Avez-vous été en mesure d'essayer de l'exemple?
post le JSP stacktrace s'il vous plaît 🙂
Ajouté stacktrace exception. Exception est à partir de github exemple. HTH
OriginalL'auteur java_dude | 2013-01-09
Vous devez vous connecter pour publier un commentaire.
Le problème est en fait que UserEntity ne possède pas de constructeur par défaut, si vous ajoutez le constructeur qu'il va travailler proprement:
OriginalL'auteur Biju Kunjummen
Je pense que ce qui se passe est que le moyen, votre formulaire est déclaré dans la page JSP, lors de la soumission, il va supposer qu'il existe un
UserEntity
exemple à la position 0 dans leusers
Liste desUserForm
exemple, mais il trébuche sur une référence null.Oui, vous ne ajouter un peu de UserEntity cas pour le modèle lorsque le formulaire est affiché (à l'aide de la loadForm méthode qui est mappé à HTTP Get), mais lorsque vous soumettez le formulaire, un nouveau modèle objet est créé, et cela on aura la valeur null dans la Liste des utilisateurs.
Essayer de rendre le modèle de session de l'étendue de cette façon, le modèle de l'instance que vous créez lorsque le formulaire est affiché sera réutilisée lorsque le formulaire est soumis. Ou créer votre propre mise en œuvre de la Liste qui va renvoyer une nouvelle instance de UserEntity lorsqu'un get(int position) est effectuée et il n'y a rien.
@ModelAttribute("showForm")
est fonctionne parfaitement bien. Savez-vous pourquoi ça arrive. Débogueur affiche sur soumettresetupFormObject()
est appelé à nouveau. Ce n'est un peu ce qui me confondre.Vous êtes à l'aide de @ModelAttribute en deux endroits, à la fois sur une méthode et une fois sur un champ, et je ne suis pas très sûr de celui que vous évoquez dans votre commentaire ci-dessus, mais je suppose que c'est celui sur la showForm méthode. Je pense que cela fonctionne pour vous, parce que si vous annoter la méthode comme ça, le Printemps va appeler cette méthode à chaque fois qu'il a besoin d'une instance de la "showForm" du modèle (donc la liste est renseignée ici). Sans cette annotation, le Printemps sera juste utiliser le pas de arg constructeur pour instancier le haricot pour la showForm modèle, par conséquent, toutes les valeurs null dans le champ de liste.
OriginalL'auteur Shivan Dragon