1

I have seen that annotation like @NotNull etc are given in JSR Specifications.

Now as i am currently studying Spring MVC and building website in that

so i am confused are they same or different and have they any relation with spring MVC.

because for .eg if i use @NotEmpty then will spring knows that if i leave it empty then it displays in form with error message like we code in validator and its messages

This is my method , i am confused were to add @Valid

 public String add(@ModelAttribute("personAttribute") Person person) {
        logger.debug("Received request to add new person");


        personService.add(person);

        // This will resolve to /WEB-INF/jsp/addedpage.jsp
        return "hibernate/addedpage";
    }

1 Answers1

1

Spring form validation is different, but it also support JSR-303 validation. You can validate a whole model attribute by annotating it with @Valid (as a method parameter)

public String add(@Valid @ModelAttribute("personAttribute") Person person) { .. }

You would need:

  • a JSR-303 provider on the classpath
  • <mvc:annotation-driven /> in the spring-mvc xml to enable validation
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • i have added the method code and hibernate is adding the object to mysql . now in above code where can i write the @valid –  Mar 02 '11 at 08:42
  • I tried but validation don't worked ,neither i get any error . i am using hibernate and mysql. do i need to use BIndingresult in that method. also does i need to use hibernate validator or spring validator. Is there any special jar for JSR or it comes with hibernate –  Mar 02 '11 at 09:00
  • 1
    @Name You need a JSR-303 validator on your classpath, as Bozho said. Hibernate Validator is a good choice, but note that it's separate from Hibernate. You'll need to include it too. – GaryF Mar 02 '11 at 09:09
  • i have done that but still form is not validating. my data goses in database even if its empty. i have used @NotNull . i have included javax.validation.contraint.NotNull and in form.jsp i have –  Mar 02 '11 at 09:20