我的“用户”对象在注册到数据库之前被验证时遇到问题
使用的技术: - 带有 Hibernate Validator 的 Spring Web MVC - Spring Data (JDBC) - Maven
这是我的控制器代码中的一个片段:
@RequestMapping(method = RequestMethod.POST,params = "add")
public ModelAndView add(@Valid @ModelAttribute("user") User user, BindingResult result) throws Exception {
if (!result.hasErrors()) { // ERRORS NEVER HAPPEN ??
userDao3.insertUser(user);
}
return returnHome();
}
我的用户验证如下:
@Size(min = 3, max = 20, message = "{required.name}")
String name;
@Min(value = 25, message = "{min.age}")
int age;
@Min(value = 2000, message = "{invalid.salary}")
float salary;
我的jsp布局如下:
<c:set var="contextPath" value="${pageContext.request.contextPath}"/>
<div class="container">
<form:form method="post" action="${contextPath}/?add" modelAttribute="user">
<div class="row">
<div class="col-15">
<tags:message code="field.username"/>
</div>
<div class="col-50">
<form:input cssClass="field" path="name"/>
</div>
<div class="col-25">
<form:errors path="name"/>
</div>
</div>
<div class="row">
<div class="col-15">
<tags:message code="field.age"/>
</div>
<div class="col-50">
<form:input cssClass="field" path="age"/>
</div>
<div class="col-25">
<form:errors path="age"/>
</div>
</div>
<div class="row">
<div class="col-15">
<tags:message code="field.salary"/>
</div>
<div class="col-50">
<form:input cssClass="field" path="salary"/>
</div>
<div class="col-25">
<form:errors path="salary"/>
</div>
</div>
<div class="row">
<input type="submit" value="Submit">
</div>
</form:form>
我的 Spring mvc 配置 xml 文件:
<mvc:annotation-driven/>
<bean name="homeController" class="Controllers.HomeController">
<property name="userDao2" ref="userDao2"/>
<property name="userDao3" ref="userDao3"/>
</bean>
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>naming</value>
<value>ValidationMessages</value>
</list>
</property>
</bean>
我的 pom 的依赖项是:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.2.5.Final</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
问题是:在调试时发现 BindingResult 对象中的错误总是等于 0。
调试结果:
请帮助我理解这个问题。