我正在使用表单验证实现示例Spring MVC表单。我有一个复杂类型Address作为Student表单bean的bean属性。并且我为@NotEmpty bean属性添加了表单验证Address。但是,UI中并未反映出同样的情况。但是,表单验证可用于Student表单bean的其他原始类型。

因此,验证对于Student表单bean来说是完美的,但不适用于Address表单bean中嵌套的复杂类型(如Student)。

我正在尝试了解原因和解决方法。

春季版本4.0+。
休眠验证器API:5.2.4

Student POJO:

package com.xyz.form.beans;

import java.util.Date;
import java.util.List;

import javax.validation.constraints.Past;
import javax.validation.constraints.Size;

import org.hibernate.validator.constraints.NotEmpty;

import com.xyz.validators.DateNotEmpty;
import com.xyz.validators.ListNotEmpty;

public class Student {
    @Size(min = 2, max = 30)
    private String firstName;
    @Size(min = 2, max = 30)
    private String lastName;
    @NotEmpty
    private String gender;
    @DateNotEmpty
    @Past
    private Date DOB;
    private String email;
    private String mobileNumber;
    @ListNotEmpty
    private List<String> courses;
    private Address address;

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    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 String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public Date getDOB() {
        return DOB;
    }

    public void setDOB(Date dOB) {
        DOB = dOB;
    }

    public String getEmail() {
        return email;
    }

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

    public String getMobileNumber() {
        return mobileNumber;
    }

    public void setMobileNumber(String mobileNumber) {
        this.mobileNumber = mobileNumber;
    }

    public List<String> getCourses() {
        return courses;
    }

    public void setCourses(List<String> courses) {
        this.courses = courses;
    }
}


Address POJO:


    包com.xyz.form.beans;

    导入org.hibernate.validator.constraints.NotEmpty;

    导入com.xyz.validators.LongNotEmpty;

    公共课程地址{
        @不是空的
        私人弦屋
        @不是空的
        私人弦街;
        @不是空的
        私有字符串区域;
        @不是空的
        私人弦城市;
        @LongNotEmpty
        私人长针;

        公共字符串getHouseNo(){
            退房
        }

        公共无效setHouseNo(String houseNo){
            this.houseNo = houseNo;
        }

        公共字符串getStreet(){
            回街
        }

        public void setStreet(String street){
            this.street =街道;
        }

        公共字符串getArea(){
            返回区域;
        }

        公共无效setArea(字符串区域){
            this.area =面积;
        }

        公共字符串getCity(){
            返回城市;
        }

        public void setCity(String city){
            this.city =城市;
        }

        公共长getPin(){
            回销
        }

        public void setPin(Long pin){
            this.pin = pin;
        }
    }



Student控制器:



    @RequestMapping(value =“ /newStudentDetails.do”,方法= RequestMethod.POST)
        公共ModelAndView newStudentDetails(
                @Valid @ModelAttribute(“ student”)com.xyz.form.beans.Student studentFormBean,
                BindingResult bindingResult){
            如果(bindingResult.hasErrors()){
                返回新的ModelAndView(“ newStudentPage”);
            }
            学生studentDto =新Student();
            studentDto.setFirstName(studentFormBean.getFirstName());
            studentDto.setLastName(studentFormBean.getLastName());
            studentDto.setGender(studentFormBean.getGender());
            studentDto.setDOB(新日期(studentFormBean.getDOB()。getTime()));
            studentDto.setEmail(studentFormBean.getEmail());
            studentDto.setMobileNumber(studentFormBean.getMobileNumber());
            StringBuilder sb = new StringBuilder();
            sb.append(studentFormBean.getAddress()。getHouseNo()+“,”);
            sb.append(studentFormBean.getAddress()。getStreet()+“,”);
            sb.append(studentFormBean.getAddress()。getArea()+“,”);
            sb.append(studentFormBean.getAddress()。getCity()+“-”);
            sb.append(studentFormBean.getAddress()。getPin());
            studentDto.setAddress(sb.toString());
            studentDto.setCourses(studentFormBean.getCourses());
            studentDao.createStudent(studentDto);
            ModelAndView mav =新的ModelAndView(“ newStudentSuccess”);
            返回mav;
        }



谢谢,
维斯瓦纳特

最佳答案

您需要使用@Valid注释复杂类型。
This is the reference(引用here

09-12 22:54