views:

420

answers:

3

I want to know what is the best way to validate a submition of an html form with Spring 3.0. The validations are simple mandatory checks, not very business oriented. I'm currently using spring-modules-validator + commons-validator, but it's maven dep. is with Spring 2.0 , not 3.0. I'm not using annotations.

+2  A: 

You mentioned that you're not currently using annotations. If you're open to using them, I recommend the new bean validation API (JSR 303). Hibernate provides an implementation, and Spring 3.0 supports it. The annotations provided in javax.validation cover most basic single-field validation, and you can apply them to any POJO (not just form beans or model instances).

Eric Rath
+1  A: 

In Spring 3 the best way to go is to use annotations, if you can switch. The integration with the bean validation API makes it easy to add validation. All you have to do to validate a form bean is to add the @Valid (see "5.7.4.1 Triggering @Controller Input Validation" in the Spring MVC docs chapter "5. Validation, Data Binding, and Type Conversion") annotation to a controller method argument. Spring will then validate the bean and put the validation errors into a BindingResult for you.

@Controller
public class RegisterFormController {
  ...
  @RequestMapping(value = "/register", method = RequestMethod.POST)
  public String submitFormHandler(@ModelAttribute("registration") @Valid User user, BindingResult errors) {
    if (errors.hasErrors()) {
      // send user back to form view
    } else {
      // handle form submission
    }
  }
  ...
}

The docs say that Spring will automatically set up the bean validation factories and validators, but if that doesn't work for you, you can manually configure it very easily:

  <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="webBindingInitializer">
      <!-- Configures Spring MVC DataBinder instances -->
      <bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
      <property name="validator" ref="validator" />
      </bean>
    </property>
  </bean>

  <!-- Creates the JSR-303 Validator -->
  <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />

I also created a project based on the client-side implementation of "valang" validation from the validation Spring Module that can provide client-side validation in JavaScript based on the JSR-303 and Hibernate Validator validation annotations.

http://kenai.com/projects/jsr303js

The project provides a taglib/JavaScript file that you include to put the main JavaScript code into the page, then another tag that you put somewhere in your Spring <form:form></form:form> tag to trigger the validation.

<%@ taglib prefix="jsr303js" uri="http://kenai.com/projects/jsr303js/" %>
  ...
  <head>
    ...
    <script type="text/javascript" src="<c:url value="/js/jsr303js-codebase.js"/>"></script>
    ...
  </head>
  <body>
    ...
    <form:form name="regForm" commandName="registration" method="post">
      ...
      <jsr303js:validate commandName="registration"/>
      ...
      </form:form>
sdouglass
A: 

Here is an example I wrote on rich, client-side validation.

If you're looking for server-side validation, see here.

James Earl Douglas