简体   繁体   中英

jsr-303 validation errors on entity not displaying out in jsp

I have the following entity that is supposed to be validated when inputs are entered in forms . I can see when there are errors , its simply returns to same form(which is intended) but does not display the error messages on get properties annotated in the entity. I have the {< mvc:annotation />}

in my disptacher 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;
    }

}

And my controller is following:

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";
        }
    }

}

And I am using javax validation. My jsp is follow :

<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>

In your Jsp, you should have <form:error path="image"/> . When there is error in <form:input path="image" /> , it will appear in the <form:error> tag.

Follow this tutorial: http://www.mkyong.com/spring-mvc/spring-mvc-form-errors-tag-example/

我认为您应该将@ NotBlank,@ Email等验证注释放在属性上而不是它们的getter方法上。如果我错了,请忽略。

The problem is in "redirect". Validation error messages are placed to request attributes but no request attributes will survive with redirect

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

please remove redirect: and check

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM