Spring 3 MVC: Show validation message with custom validator

后端 未结 1 1145
灰色年华
灰色年华 2020-12-23 22:41

I need help. I am beginner in jsp, MVC. I want to validate form input with custom validator in Spring 3 MVC.

My validator class

   package validator         


        
相关标签:
1条回答
  • 2020-12-23 23:34

    This happens because of mismatch between default model attribute names in view and controller:

    • When you write <form:form> without modelAttribute (or commandName) attribute, it uses default model attribute name command.
    • When you write @ModelAttribute UserModel user in your controller, it assumes that the name of this attribute is a decapitalized class name, i.e. userModel.

    That is, error messages produced by validator are bound to model attribute named userModel, while your view tries to show errors for model attribute command.

    You need to set a model attribute name explicitly, either in the view (<form:form modelAttribute = "userModel" ...>) or in the controller (@ModelAttribute("command")).

    0 讨论(0)
提交回复
热议问题