【问题标题】:Spring Validation Framework not workingSpring验证框架不起作用
【发布时间】:2014-08-24 23:22:15
【问题描述】:

我试图在 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>

【问题讨论】:

  • 您可能希望减少呈现给期望答案的人的代码量。这很长。

标签: java spring hibernate validation spring-mvc


【解决方案1】:

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

<bean id="validator" class=
    "org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>

在您的 Spring 配置中。

【讨论】:

  • 是的!即使在配置了 bean 验证后它也不起作用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-07
  • 1970-01-01
  • 1970-01-01
  • 2020-05-25
  • 2017-05-17
相关资源
最近更新 更多