@Range 在 spring mvc 中不工作
@Range not working in spring mvc
我正在使用 spring mvc 并尝试通过在我的模型 Goal
中使用 @Range
来验证表单 addGoal.jsp
中的用户输入。但是如果我输入值例如 223
大于 120
,因为我在注释中设置它打印 Result Errors false
(因为我在控制器中的 updateGoal 方法中打印)并且没有路由到 addGoal
视图我似乎无法理解为什么它不验证 result.hasErrors()
给我错误,即使我输入的值不在范围内。
型号
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,以便任何人都可以看到。
如有任何帮助,我们将不胜感激。
谢谢
在您的 pom 中,您有以下依赖关系
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.2.0.Final</version>
<scope>provided</scope>
</dependency>
罪魁祸首是<scope>provided</scope>
。要使 Bean Validation 工作,必须在 class 路径上有一个实现。编码时是这种情况,但部署时不是这种情况(因为依赖项未打包在 war 中)。
如果有可用的 JSR-303 实现,Spring 只会注册一个 LocalValidatorFactoryBean
,否则它不会注册,验证也不起作用。
修复方法是简单地删除 <scope>provided</scope>
。
即使在删除范围后我也遇到了这个问题 tag.Then 我从本地存储库中删除了休眠文件夹 C:\Users\computerName\.m2\repository\org\hibernate 然后使用了 maven update下载与休眠相关的所有 jar again.Now 它工作正常。
我正在使用 spring mvc 并尝试通过在我的模型 Goal
中使用 @Range
来验证表单 addGoal.jsp
中的用户输入。但是如果我输入值例如 223
大于 120
,因为我在注释中设置它打印 Result Errors false
(因为我在控制器中的 updateGoal 方法中打印)并且没有路由到 addGoal
视图我似乎无法理解为什么它不验证 result.hasErrors()
给我错误,即使我输入的值不在范围内。
型号
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,以便任何人都可以看到。
如有任何帮助,我们将不胜感激。
谢谢
在您的 pom 中,您有以下依赖关系
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.2.0.Final</version>
<scope>provided</scope>
</dependency>
罪魁祸首是<scope>provided</scope>
。要使 Bean Validation 工作,必须在 class 路径上有一个实现。编码时是这种情况,但部署时不是这种情况(因为依赖项未打包在 war 中)。
Spring 只会注册一个 LocalValidatorFactoryBean
,否则它不会注册,验证也不起作用。
修复方法是简单地删除 <scope>provided</scope>
。
即使在删除范围后我也遇到了这个问题 tag.Then 我从本地存储库中删除了休眠文件夹 C:\Users\computerName\.m2\repository\org\hibernate 然后使用了 maven update下载与休眠相关的所有 jar again.Now 它工作正常。