Java Spring验证框架不工作
我试图在Spring应用程序上实现Hibernate验证。我已经为数据模型中的变量插入了适当的注释。并在控制器中使用@Valid。运行应用程序时,不会调用验证,而是直接将数据存储到数据库中 //模范班Java Spring验证框架不工作,java,spring,hibernate,validation,spring-mvc,Java,Spring,Hibernate,Validation,Spring Mvc,我试图在Spring应用程序上实现Hibernate验证。我已经为数据模型中的变量插入了适当的注释。并在控制器中使用@Valid。运行应用程序时,不会调用验证,而是直接将数据存储到数据库中 //模范班 package com.student.model; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.p
package com.student.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import org.hibernate.validator.constraints.NotEmpty;
@Entity
public class Student {
@Id
@Column
@GeneratedValue(strategy=GenerationType.AUTO)
private int studentId;
@Column
@NotEmpty
private String firstname;
@Column
@NotEmpty
private String lastname;
@Column
@NotEmpty
private int yearLevel;
public Student(){}
public Student(int studentId, String firstname, String lastname,
int yearLevel) {
super();
this.studentId = studentId;
this.firstname = firstname;
this.lastname = lastname;
this.yearLevel = yearLevel;
}
public int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public int getYearLevel() {
return yearLevel;
}
public void setYearLevel(int yearLevel) {
this.yearLevel = yearLevel;
}
}
//控制器类
package com.student.controller;
import java.util.Map;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
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 com.student.model.Student;
import com.student.service.StudentService;
@Controller
public class StudentController {
@Autowired
private StudentService studentService;
@RequestMapping("/index")
public String setupForm(Map<String, Object> map) {
Student student = new Student();
map.put("student", student);
map.put("studentList", studentService.getAllStudent());
return "student";
}
@RequestMapping(value = "/student.do", method = RequestMethod.POST)
public String doActions(@Valid @ModelAttribute Student student,
BindingResult result, @RequestParam String action,
Map<String, Object> map) {
if (result.hasErrors()) {
return "student";
} else {
Student studentResult = new Student();
switch (action.toLowerCase()) {
case "add":
studentService.add(student);
studentResult = student;
break;
case "edit":
studentService.edit(student);
studentResult = student;
break;
case "delete":
studentService.delete(student.getStudentId());
studentResult = new Student();
break;
case "search":
Student searchedStudent = studentService.getStudent(student
.getStudentId());
studentResult = searchedStudent != null ? searchedStudent
: new Student();
break;
}
map.put("student", studentResult);
map.put("studentList", studentService.getAllStudent());
return "student";
}
}
}
package com.student.controller;
导入java.util.Map;
导入javax.validation.Valid;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.stereotype.Controller;
导入org.springframework.validation.BindingResult;
导入org.springframework.web.bind.annotation.ModelAttribute;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RequestMethod;
导入org.springframework.web.bind.annotation.RequestParam;
导入com.student.model.student;
导入com.student.service.StudentService;
@控制器
公共班级学生控制员{
@自动连线
私人学生服务学生服务;
@请求映射(“/index”)
公共字符串设置窗体(映射){
学生=新生();
地图。放置(“学生”,学生);
put(“studentList”,studentService.getAllStudent());
返回“学生”;
}
@RequestMapping(value=“/student.do”,method=RequestMethod.POST)
公共字符串doActions(@Valid@modeldattribute Student,
BindingResult,@RequestParam字符串操作,
(地图){
if(result.hasErrors()){
返回“学生”;
}否则{
Student studentResult=新学生();
开关(action.toLowerCase()){
案例“添加”:
学生服务。添加(学生);
studentResult=学生;
打破
案例“编辑”:
学生服务编辑(学生);
studentResult=学生;
打破
案例“删除”:
删除(student.getStudentId());
studentResult=新学生();
打破
案例“搜索”:
Student searchedStudent=studentService.getStudent(学生
.getStudentId());
studentResult=searchedStudent!=null?searchedStudent
:新生();
打破
}
地图放置(“学生”,studentResult);
put(“studentList”,studentService.getAllStudent());
返回“学生”;
}
}
}
student.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ include file="/WEB-INF/jsp/includes.jsp"%>
<!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>Student Management</title>
</head>
<body>
<div id="container">
<div id="header">
<h1>Students Data</h1>
</div>
<form:form action="student.do" method="POST" commandName="student" >
<table>
<tr>
<td>Student ID</td>
<td><form:input path="studentId" /><form:errors path="studentId"/></td>
</tr>
<tr>
<td>First name</td>
<td><form:input path="firstname" /><form:errors path="firstname"/></td>
</tr>
<tr>
<td>Last name</td>
<td><form:input path="lastname" /><form:errors path="lastname"/></td>
</tr>
<tr>
<td>Year Level</td>
<td><form:input path="yearLevel" /><form:errors path="yearLevel"/></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" name="action" value="Add" />
<input type="submit" name="action" value="Edit" />
<input type="submit" name="action" value="Delete" />
<input type="submit" name="action" value="Search" />
</td>
</tr>
</table>
</form:form>
<br>
<table border="1">
<th>ID</th>
<th>First name</th>
<th>Last name</th>
<th>Year level</th>
<c:forEach items="${studentList}" var="student">
<tr>
<td>${student.studentId}</td>
<td>${student.firstname}</td>
<td>${student.lastname}</td>
<td>${student.yearLevel}</td>
</tr>
</c:forEach>
</table>
</div>
</body>
</html>
学生管理
学生数据
学生证
名字
姓
年度水平
身份证件
名字
姓
年度水平
${student.studentId}
${student.firstname}
${student.lastname}
${student.yearLevel}
您是否配置了bean验证?您可能需要添加
<bean id="validator" class=
"org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>
在Spring配置中。您可能希望减少向期望得到答案的人提供的代码量。这相当长。是的!即使在配置bean验证之后,它也不起作用