Java jsr-303实体上的验证错误未显示在jsp中

Java jsr-303实体上的验证错误未显示在jsp中,java,spring,spring-mvc,bean-validation,Java,Spring,Spring Mvc,Bean Validation,我有以下实体,当输入输入到表单中时,应该对其进行验证。我可以看到,当出现错误时,它只是返回到相同的形式(这是预期的),但不会在实体中注释的get属性上显示错误消息。我有{} 在我的Dispatcher servlet中 package com.web.portal.entity; import java.io.Serializable; import org.hibernate.validator.Pattern; import org.hibernate.validator.Size;

我有以下实体,当输入输入到表单中时,应该对其进行验证。我可以看到,当出现错误时,它只是返回到相同的形式(这是预期的),但不会在实体中注释的get属性上显示错误消息。我有{}

在我的Dispatcher servlet中

package com.web.portal.entity;

import java.io.Serializable;


import org.hibernate.validator.Pattern;
import org.hibernate.validator.Size;
import org.hibernate.validator.constraints.NotBlank;
import org.hibernate.validator.constraints.Email;

@Entity
@Table(name = "faculty")
public class Faculty implements Serializable {
    /**
     *
     */
    private static final long serialVersionUID = 1L;

    private int id;

    private byte[] image;

    private String firstName;

    private String lastName;

    private String description;

    private String email;

    private String phone;

    private String research;
    private byte[] map;

    @GeneratedValue
    @Id
    @Column(name = "id")
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Column(name = "image")
    public byte[] getImage() {
        return image;
    }

    public void setImage(byte[] image) {
        this.image = image;
    }

    @NotBlank(message = "Field value should not be null")
    @Column(name = "first_name")
    @Pattern(regex = "[a-z-A-Z]*", message = "First name has invalid characters")
    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    @Column(name = "last_name")
    @NotBlank(message = "Field value should not be null")
    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @Column(name = "description")
    @NotBlank(message = "Field value should not be null")
    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Column(name = "email")
    @NotBlank(message = "Field value should not be null")
    @Email(message = "You provided an invalid email")
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Size(min = 10, max = 10, message = "Size should be ten : 4537653456")
    @Column(name = "phone_number")
    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    @Column(name = "research")
    public String getResearch() {
        return research;
    }

    public void setResearch(String research) {
        this.research = research;
    }

    @Column(name = "location_map")
    public byte[] getMap() {
        return map;
    }

    public void setMap(byte[] map) {
        this.map = map;
    }

}
我的控制器如下:

package com.web.portal.controller;

import java.util.Map;

import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.web.portal.entity.Faculty;
import com.web.portal.service.FacultyService;

@Controller
public class AdminManagerController {
    private static final Logger LOG = Logger.getLogger(AdminManagerController.class);
    @Autowired
    FacultyService service;

    public FacultyService getService() {
        return service;
    }

    public void setService(FacultyService service) {
        this.service = service;
    }

    @RequestMapping(value = "/manager.do")
    public String facultyLists(Map<String, Object> map) {
        Faculty faculty = new Faculty();
        map.put("facultyList", service.getList(faculty));
        return "faculty_and_staff";
    }

    @RequestMapping(value = "/fillInformation.do", method = RequestMethod.GET)
    public String fillFaculty(ModelMap model) {
        model.addAttribute("faculty", new Faculty());
        return "add_faculty_info";
    }

    @RequestMapping(value = "/addFaculty.do", method = RequestMethod.POST)
    public String addFaculty(@Valid Faculty faculty, BindingResult result, Map model) {

        if (result.hasErrors()) {
            return "redirect:/fillInformation.do";
        } else {
            service.addFaculty(faculty);
            return "redirect:/manager.do";
        }
    }

}
包com.web.portal.controller;
导入java.util.Map;
导入javax.validation.Valid;
导入org.apache.log4j.Logger;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.stereotype.Controller;
导入org.springframework.ui.ModelMap;
导入org.springframework.validation.BindingResult;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RequestMethod;
导入com.web.portal.entity.Faculty;
导入com.web.portal.service.FacultyService;
@控制器
公共类AdminManagerController{
私有静态最终记录器LOG=Logger.getLogger(AdminManagerController.class);
@自动连线
教师服务;
公共FacultyService getService(){
回程服务;
}
公共无效设置服务(FacultyService服务){
服务=服务;
}
@请求映射(value=“/manager.do”)
公共字符串功能列表(映射){
教员=新教员();
map.put(“facultyList”,service.getList(faculty));
返回“教职员工”;
}
@RequestMapping(value=“/fillInformation.do”,method=RequestMethod.GET)
公共字符串(模型映射模型){
model.addAttribute(“faculty”,新faculty());
返回“添加教员信息”;
}
@RequestMapping(value=“/addFaculty.do”,method=RequestMethod.POST)
公共字符串addFaculty(@Valid Faculty,BindingResult,Map model){
if(result.hasErrors()){
返回“重定向:/fillInformation.do”;
}否则{
服务.学院(教职员);;
返回“重定向:/manager.do”;
}
}
}
我正在使用javax验证。我的建议如下:

<body>
<div class="container">
<div class="login">
<h1>Add Faculty</h1>
<form:form method="POST" action="addFaculty.do"  commandName="faculty">
<form:errors path="*" /> 
 <table>
<tr>
    <td><form:label path="image"></form:label>Picture</td>
    <td><form:input path="image" placeholder="" accept="image/*" type="file"/></td>

  </tr>
  <tr>
    <td><form:label path="firstName"></form:label> Firstname</td>
    <td><form:input path="firstName" placeholder=""/></td>

加教员
图画
名字

在Jsp中,您应该
。 当
中出现错误时,它将出现在
标记中


遵循本教程:

我认为您应该在属性上放置@NotBlank、@Email等验证注释,而不是它们的getter方法。。如果我错了,就别理我

问题在于“重定向”。验证错误消息被放置到请求属性中,但没有请求属性会因重定向而继续存在

if (result.hasErrors()) {
    return "redirect:/fillInformation.do";
}

请删除
重定向:
并检查谢谢CodeBender的回复。如果你看我的jsp,我有通配符。这应该能捕捉到路径上的所有错误。啊,我的错。如果控制器方法的参数中有一个模型,那么在重定向回jsp?之前使用Model.addAttribute(“faculty”,faculty)。我创建了一个测试项目,并尝试使用您的代码。验证工作正常。不,我没有任何错误。验证错误不会显示在视图中。目前,我已经实现了验证器来捕获错误,但我更喜欢jsr-303。如果它是在你的工作比我不知道什么是错的。这太令人沮丧了。另一个区别是我删除了重定向:。你为什么要使用重定向呢?