I am using spring mvc and trying to validate user input in a form addGoal.jsp
by using @Range
in my model Goal
.But if I enter value for ex 223
greater than 120
as I set in the annotation it prints Result Errors false
(as I printed in updateGoal method in controller) and not routing to addGoal
view I cant seem to understand why its not validating the result.hasErrors()
giving me false even if I enter value that are not in range.
Model
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;
}
}
Controller
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>
I have posted my xmls in order anyone wants to see.
Any help would be appreciated.
Thanks
In your pom you have the following dependency
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.2.0.Final</version>
<scope>provided</scope>
</dependency>
The culprit is <scope>provided</scope>
. For Bean Validation to work there has to be an implementation on the class path. This is the case while coding, however it isn't the case when deployed (as the dependency isn't packaged in the war).
Spring will only register a LocalValidatorFactoryBean
if there is a JSR-303 implementation available else it won't and the validations don't work.
The fix is to simply remove <scope>provided</scope>
.
即使删除了作用域标记,我仍然面临着这个问题。然后我从本地存储库(即C:\\ Users \\ computerName \\ .m2 \\ repository \\ org \\ hibernate)删除了hibernate文件夹,然后使用maven update下载了所有与再次休眠。现在一切正常。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.