Spring Validation Framework无法正常工作

时间:2014-07-03 17:59:27

标签: 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.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";
        }
    }
}

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>

1 个答案:

答案 0 :(得分:0)

您是否配置了bean验证?您可能需要添加

<bean id="validator" class=
    "org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>
在你的Spring配置中