我正在使用Spring MVC和Thymeleaf。在此,JSR验证无效。当我使用JSP时,它会显示JSR验证消息,但是当我使用Thymeleaf验证时,则不会发生。仅供参考,我在下面的链接https://www.thymeleaf.org/doc/tutorials/3.0/thymeleafspring.pdf中进行了验证 我尝试了很多方法,但问题仍然存在。
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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.kalavakuri</groupId>
<artifactId>springmvcthemeleaf</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<!-- Spring MVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
<version>3.0.9.RELEASE</version>
</dependency>
<!-- Spring MVC -->
<!-- JSR 303/349 Validations -->
<dependency>
<groupId>com.fasterxml</groupId>
<artifactId>classmate</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.9.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
<version>3.3.2.Final</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
<!-- JSR 303/349 Validations -->
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
springmvc.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="com.kalavakuri"></context:component-scan>
<bean id="templateResolver" class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
<property name="prefix" value="/WEB-INF/templates/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML" />
<property name="cacheable" value="false" />
</bean>
<bean id="templateEngine" class="org.thymeleaf.spring5.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
<property name="enableSpringELCompiler" value="true" />
</bean>
<bean class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
</bean>
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:student"/>
<property name="cacheSeconds" value="1"/> <!-- Do not use it in production, it causes performance issue -->
</bean>
</beans>
控制器类:
package com.kalavakuri.controllers;
import java.util.ArrayList;
import java.util.List;
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.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import com.kalavakuri.model.Country;
import com.kalavakuri.model.StudentVO;
@Controller
@SessionAttributes("studentVO")
public class StudentController {
@RequestMapping(value="/", method=RequestMethod.GET)
public String welcomeController(Model model) {
StudentVO studentVO = new StudentVO();
model.addAttribute("studentVO", studentVO);
prepareCountries(studentVO);
return "Index";
}
@RequestMapping(value="/registerStudent", params= {"save"}, method=RequestMethod.POST)
public String loginStudent(@Valid @ModelAttribute("studentVO") StudentVO studentVO, BindingResult bindingResult) {
if(!bindingResult.hasErrors()) {
StudentVO vo = new StudentVO();
vo.setStudentId(studentVO.getStudentVOs().size()+1);
vo.setUserName(studentVO.getUserName());
vo.setRemember(studentVO.getRemember());
vo.setPreferredCountry(studentVO.getPreferredCountry());
vo.setIsHomeCountry(studentVO.getIsHomeCountry());
studentVO.getStudentVOs().add(vo);
}
return "Index";
}
@RequestMapping(value="/registerStudent", params= {"removeRow"}, method=RequestMethod.POST)
public String removeStudent(@Valid @ModelAttribute("studentVO") StudentVO studentVO, @RequestParam("removeRow") int studentId) {
studentVO.getStudentVOs().remove(studentId);
return "Index";
}
/**
* @param studentVO
*/
private void prepareCountries(StudentVO studentVO) {
List<Country> countries = new ArrayList<Country>();
Country country1 = new Country();
country1.setCountryCode("IN");
country1.setCountryName("India");
Country country2 = new Country();
country2.setCountryCode("US");
country2.setCountryName("United States Of Ameriac");
countries.add(country1);
countries.add(country2);
studentVO.setCountries(countries);
}
}
Index.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Student Registration::</title>
</head>
<body>
<form action="#" th:action="@{registerStudent}" th:object="${studentVO}" method="post" id="studentVO">
<h2 style="padding-left: 750px">Welcome to Spring MVC Thymeleaf</h2>
<table style="padding-left: 750px;">
<tbody>
<tr>
<td>User Name:</td>
<td><input type="text" th:field="*{userName}" th:class="${#fields.hasErrors('userName')}? fieldError"></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" th:field="*{password}"></td>
</tr>
<tr>
<td>You Want Me To Remember:</td>
<td><input type="checkbox" th:field="*{remember}"></td>
</tr>
<tr>
<td>Select Preferred Country:</td>
<td>
<select th:field="*{preferredCountry}">
<option value="Select">Select</option>
<option th:each="country: *{countries}" th:value="${country.countryCode}" th:text="${country.countryName}"></option>
</select>
</td>
</tr>
<tr>
<td>Is Selected Country, Home Country</td>
<td><input type="radio" th:field="*{isHomeCountry}" th:value="Yes"> Yes <input type="radio" th:field="*{isHomeCountry}" th:value="No"> No</td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td align="left"><button name="save" type="submit" th:text="Register"></button>  <input type="reset" th:value="Reset"> </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</tbody>
</table>
<div style="padding-left: 750px;">
<table border="1" style="border-color: black;border-collapse: collapse;">
<thead>
<tr>
<th>Student ID</th>
<th>User Name</th>
<th>Want Me To Remember</th>
<th>Preferred Country</th>
<th>Is Home Country</th>
<th>Delete Operation</th>
</tr>
</thead>
<tbody>
<tr th:each="student, rowStat : ${studentVO.studentVOs}">
<td th:text="${student.studentId}"></td>
<td th:text="${student.userName}"></td>
<td th:text="${student.remember}"></td>
<td th:text="${student.preferredCountry}"></td>
<td th:text="${student.isHomeCountry}"></td>
<td><button type="submit" name="removeRow" th:value="${rowStat.index}">Remove row</button> </td>
</tr>
</tbody>
</table>
</div>
</form>
</body>
</html>
Bean:
package com.kalavakuri.model;
import java.util.ArrayList;
import java.util.List;
import javax.validation.constraints.Size;
public class StudentVO {
private long studentId;
private String studentName;
private String studentAge;
@Size(min=3,max=10,message="User name should be minimum 3 characters and maximum 10 characters")
private String userName;
private String password;
private Boolean remember;
private String preferredCountry;
private List<Country> countries;
private String isHomeCountry;
private List<StudentVO> studentVOs = new ArrayList<StudentVO>();
/**
* @return the studentId
*/
public long getStudentId() {
return studentId;
}
/**
* @param studentId the studentId to set
*/
public void setStudentId(long studentId) {
this.studentId = studentId;
}
/**
* @return the studentName
*/
public String getStudentName() {
return studentName;
}
/**
* @param studentName the studentName to set
*/
public void setStudentName(String studentName) {
this.studentName = studentName;
}
/**
* @return the studentAge
*/
public String getStudentAge() {
return studentAge;
}
/**
* @param studentAge the studentAge to set
*/
public void setStudentAge(String studentAge) {
this.studentAge = studentAge;
}
/**
* @return the userName
*/
public String getUserName() {
return userName;
}
/**
* @param userName the userName to set
*/
public void setUserName(String userName) {
this.userName = userName;
}
/**
* @return the password
*/
public String getPassword() {
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
/**
* @return the remember
*/
public Boolean getRemember() {
return remember;
}
/**
* @param remember the remember to set
*/
public void setRemember(Boolean remember) {
this.remember = remember;
}
/**
* @return the preferredCountry
*/
public String getPreferredCountry() {
return preferredCountry;
}
/**
* @param preferredCountry the preferredCountry to set
*/
public void setPreferredCountry(String preferredCountry) {
this.preferredCountry = preferredCountry;
}
/**
* @return the countries
*/
public List<Country> getCountries() {
return countries;
}
/**
* @param countries the countries to set
*/
public void setCountries(List<Country> countries) {
this.countries = countries;
}
/**
* @return the isHomeCountry
*/
public String getIsHomeCountry() {
return isHomeCountry;
}
/**
* @param isHomeCountry the isHomeCountry to set
*/
public void setIsHomeCountry(String isHomeCountry) {
this.isHomeCountry = isHomeCountry;
}
/**
* @return the studentVOs
*/
public List<StudentVO> getStudentVOs() {
return studentVOs;
}
/**
* @param studentVOs the studentVOs to set
*/
public void setStudentVOs(List<StudentVO> studentVOs) {
this.studentVOs = studentVOs;
}
}