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: addValidators accepts more than one parameter (oval)

If you select 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)); }.}}  

As you see setValidator clears the current (default) verifier, so the @valid annotation Expected as will not work.


Comments

Popular posts from this blog

sqlite3 - UPDATE a table from the SELECT of another one -

c# - Showing a SelectedItem's Property -

javascript - Render HTML after each iteration in loop -