我使用Spring MVC和試圖驗證用戶輸入的形式addGoal.jsp
在我的模型Goal
。但使用@Range
如果我對前223
大於120
輸入值,因爲我在註釋設置它打印Result Errors false
(因爲我在控制器updateGoal方法中打印),而不是路由到addGoal
視圖我似乎不明白爲什麼它不驗證result.hasErrors()
即使我輸入不在範圍內的值,我也是虛假的。@range不是Spring MVC中工作
模型
import org.hibernate.validator.constraints.Range;
public class Goal {
@Range(min = 1,max = 120) // validation not working
private int minutes;
public int getMinutes() {
return minutes;
}
public void setMinutes(int minutes) {
this.minutes = minutes;
}
}
控制器
import javax.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import testspring.model.Goal;
@Controller
@SessionAttributes("goal")
public class GoalController {
@RequestMapping(value = "/addGoal",method = RequestMethod.GET)
public String addGoal(Model model){
Goal g=new Goal();
g.setMinutes(10);
model.addAttribute("goal", g);
return "addGoal";
}
@RequestMapping(value = "/addGoal",method = RequestMethod.POST)
public String updateGoal(@Valid @ModelAttribute ("goal") Goal goal,BindingResult result){
System.out.println("Result Errors "+result.hasErrors());
System.out.println("Minutes Updated "+goal.getMinutes());
if(result.hasErrors())
return "addGoal";
return "redirect:addMinutes.html";
}
}
addGoal.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form:form commandName="goal">
<table>
<tr>
<td>Enter Minutes</td>
<td><form:input path="minutes"/></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Enter Goal Minutes">
</td>
</tr>
</table>
</form:form>
</body>
</html>
servlet的config.xml中
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="testspring" />
<mvc:annotation-driven />
<mvc:resources location="pdfs" mapping="/pdfs/**"/>
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="language"></property>
</bean>
</mvc:interceptors>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
<property name="defaultLocale" value="en"></property>
</bean>
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource" >
<property name="basename" value="messages"></property>
</bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
的pom.xml
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>testspring</groupId>
<artifactId>FitnessTracker</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>FitnessTracker Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.2.0.Final</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>FitnessTracker</finalName>
</build>
</project>
我已經張貼了我個XML爲了任何人希望看到的。
任何幫助,將不勝感激。
感謝
您似乎沒有在任何地方定義的驗證器bean。 – chrylis
提供了您的hibernate-validator刪除範圍。 –