java - Spring MVC validator annotation + custom validation -
I am working on Spring MVC application, where I need verification based on Spring MVC videor. For this I added an annotation to the class and setup controller in the first stage and it works fine and now I need to implement the custom verifier to execute complex arguments, but I want to use the existing annotation and just I would like to add additional checks.
My user class:
public class user {@NotEmpty private string name; @ Private entry is not logged; // should be unique}
My validator:
@Component public class UserValidator implementer verifier {@Autowired Private UserDAO userDAO; Support for @ Override Public Boolean (class
My Controller:
@ Controller Public Class UserController {@Autowired Private UserValidator validator; @InitBinder Secure Zero InitBender (Last WebDetBender Binder) {binder.setValidator (validator); } @RequestMapping (Value = "/ save") Public ModelAndView (@ Valid @ModelAttribute ("User") End User User, Binding Results Results) {if (result.hasErrors ()) {// handle error} else {// save user}}}
So, is it possible to use custom medicine and annotation? And if yes, how are you?
I know this is an old question, but, for Google ...
You should use addValidators
instead of setValidator
. Like the following:
@InitBinder Secure Zero InitBender (Last WebDetBender Binder) {binder.addValidators (yourCustomValidator, anotherValidatorOfYours); }
PS: If you select As you see addValidators accepts more than one parameter (oval)
org.springframework.validation Check the source. Data Binder
You will see:
Public class database banner applies Property Editor Registry, TypeConverter {.... Public Zero Set Validator (Accredited Verifier) {Achievement Validator}; This.validators.clear (); This.validators.add (validator); } Public Zero addValidators (Validator ... validators) {assertValidators (validators); This.validators.addAll (Arrays.asList (Authenticators)); }.}}
setValidator
clears the current (default) verifier, so the @valid
annotation Expected as will not work.
Comments
Post a Comment