问题描述:

I'm using Java 1.7.0_09, Spring 4 totally xml-free, Intellij Idea 13.1.6 and Tomcat 1.0.151. I have problem with jsr 303 validation. I'm trying to validate form passed to spring via POST method. Here is my code:

Controller:

 @RequestMapping(value="/register", method=POST)

public String processRegistration(@Valid Spitter spitter, BindingResult bindingResult) {

if (bindingResult.hasErrors()) {

return "registerForm";

}

repository.save(spitter);

return "redirect:/spitter/" + spitter.getUsername();

}

Here is my bean:

public class Spitter {

private Long id;

@NotNull

@Size(min=5, max=16)

private String username;

@NotNull

@Size(min=5, max=25)

private String password;

@NotNull

@Size(min=2, max=30)

private String firstName;

@NotNull

@Size(min=2, max=30)

private String lastName;

private String email;

public Spitter() {}

public Spitter(String username, String password, String firstName, String lastName, String email) {

this(null, username, password, firstName, lastName, email);

}

public Spitter(Long id, String username, String password, String firstName, String lastName, String email) {

this.id = id;

this.username = username;

this.password = password;

this.firstName = firstName;

this.lastName = lastName;

this.email = email;

}

}

Form:

<%@ taglib uri="http://www.springframework.org/tags/form" prefix="sf" %>

<sf:form method="POST" commandName="spitter">

First name: <sf:input path="firstName"/><br/>

<sf:errors path="firstName" /><br/>

Last name: <sf:input path="lastName"/><br/>

Email: <sf:input path="email"/><br/>

Username: <sf:input path="username"/><br/>

Password: <sf:input path="password"/><br/>

<input type="submit" value="Register"/>

</sf:form>

Maven file:

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>groupId</groupId>

<artifactId>SpringTest</artifactId>

<version>1.0-SNAPSHOT</version>

<dependencies>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-webmvc</artifactId>

<version>4.1.2.RELEASE</version>

</dependency>

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>javax.servlet-api</artifactId>

<version>3.1.0</version>

<scope>provided</scope>

</dependency>

<dependency>

<groupId>jstl</groupId>

<artifactId>jstl</artifactId>

<version>1.2</version>

<scope>provided</scope>

</dependency>

<dependency>

<groupId>org.apache.commons</groupId>

<artifactId>commons-lang3</artifactId>

<version>3.3.2</version>

</dependency>

<dependency>

<groupId>org.mockito</groupId>

<artifactId>mockito-all</artifactId>

<version>1.10.8</version>

</dependency>

<dependency>

<groupId>org.hibernate</groupId>

<artifactId>hibernate-validator</artifactId>

<version>5.1.3.Final</version>

</dependency>

<dependency>

<groupId>javax.el</groupId>

<artifactId>javax.el-api</artifactId>

<version>3.0.0</version>

</dependency>

<dependency>

<groupId>javax.validation</groupId>

<artifactId>validation-api</artifactId>

<version>1.1.0.Final</version>

</dependency>

<dependency>

<groupId>commons-logging</groupId>

<artifactId>commons-logging</artifactId>

<version>1.1</version>

</dependency>

</dependencies>

</project>

My Tomcat lib jars:

annotations-api.jar

catalina-ant.jar

catalina-ha.jar

catalina-storeconfig.jar

catalina-tribes.jar

catalina.jar

ecj-4.4.jar

el-api.jar

jasper-el.jar

jasper.jar

jsp-api.jar

jstl-1.2.jar

servlet-api.jar

slf4j-api-1.7.9.jar

tomcat-api.jar

tomcat-coyote.jar

tomcat-dbcp.jar

tomcat-i18n-es.jar

tomcat-i18n-fr.jar

tomcat-i18n-ja.jar

tomcat-jdbc.jar

tomcat-jni.jar

tomcat-spdy.jar

tomcat-util-scan.jar

tomcat-util.jar

tomcat-websocket.jar

websocket-api.jar

Validation seems not firing at all. Regardless of input put in form condition

if (bindingResult.hasErrors())

is always false.

To debug this I write some unit test. Here it is:

@Test

public void carIsValid() {

Spitter car = new Spitter("aaaaaa","aaaaaa","aaaaaa","aaaaaa","aaaaaa");

ValidatorFactory factory = Validation.buildDefaultValidatorFactory();

Validator validator = factory.getValidator();

Set<ConstraintViolation<Spitter>> constraintViolations = validator.validate(car);

assertEquals(0, constraintViolations.size());

}

And here validation works like a charm. Because of that I try to validate input form like that so I change my controller code to:

@RequestMapping(value="/register", method=POST)

public String processRegistration(Spitter spitter) {

ValidatorFactory factory = Validation.buildDefaultValidatorFactory();

Validator validator = factory.getValidator();

if (validator.validate(spitter).size() > 0) {

return "registerForm";

}

repository.save(spitter);

return "redirect:/spitter/" + spitter.getUsername();

}

But now I get error:

java.lang.NoClassDefFoundError: javax/validation/Validation

Could you tell me why validation dont work automatically and why when I try to validate it manually it works only in unit test and crashes in controller code?

网友答案:

You are mixing Spring bean validation and JSR-303 validation.

I had this same issue and it was solved by following the tutorial referenced in this answer: Using both JSR-303 and Traditional Bean Validation?

相关阅读:
Top