zoukankan      html  css  js  c++  java
  • spring 4.0 注解数据验证2

    在spring 4.0 注解数据验证1中有基本的数据验证方法。还是那个POJO:

    package com.suyin.pojo;
    
    import java.lang.reflect.Field;
    
    import org.hibernate.validator.constraints.NotBlank;
    
    public class People {
    
        private int id;
    
        @NotBlank
        private String name;
    
        private String address;
    
        private int age;
    
        public final int getId() {
            return id;
        }
    
        public final void setId(int id) {
            this.id = id;
        }
    
        public final String getName() {
            return name;
        }
    
        public final void setName(String name) {
            this.name = name;
        }
    
        public final String getAddress() {
            return address;
        }
    
        public final void setAddress(String address) {
            this.address = address;
        }
    
        public final int getAge() {
            return age;
        }
    
        public final void setAge(int age) {
            this.age = age;
        }
    
        @Override
        public String toString() {
            return "People [id=" + id + ", name=" + name + ", address=" + address
                    + ", age=" + age + "]";
        }
    
        public People set(String name, Object obj) {
            try {
                Field f = this.getClass().getDeclaredField(name);
                f.setAccessible(true);
                f.set(this, obj);
            } catch (NoSuchFieldException | SecurityException
                    | IllegalArgumentException | IllegalAccessException e) {
                e.printStackTrace();
            }
            return this;
        }
    
    }
    View Code

    有如下需求: 添加时要保证name不为空;修改时要保证name和address不为空。这怎么办?

    spring4.0 提供了解决方案。先写两个标志接口 First和Second,类似于java.io.Serializable,如下:

    package com.suyin.my.va;
    
    public interface First {}

    package com.suyin.my.va;
    
    public interface Second {}

    改造People如下:

    package com.suyin.pojo;
    
    import java.lang.reflect.Field;
    
    import org.hibernate.validator.constraints.Email;
    import org.hibernate.validator.constraints.NotBlank;
    
    import com.suyin.my.va.First;
    import com.suyin.my.va.Second;
    
    public class People {
    
        private int id;
    
        @NotBlank(groups= {First.class,Second.class})
        private String name;
    
        @NotBlank(groups= {Second.class})
        private String address;
    
        private int age;
    
        public final int getId() {
            return id;
        }
    
        public final void setId(int id) {
            this.id = id;
        }
    
        public final String getName() {
            return name;
        }
    
        public final void setName(String name) {
            this.name = name;
        }
    
        public final String getAddress() {
            return address;
        }
    
        public final void setAddress(String address) {
            this.address = address;
        }
    
        public final int getAge() {
            return age;
        }
    
        public final void setAge(int age) {
            this.age = age;
        }
    
        @Override
        public String toString() {
            return "People [id=" + id + ", name=" + name + ", address=" + address
                    + ", age=" + age + "]";
        }
    
        public People set(String name, Object obj) {
            try {
                Field f = this.getClass().getDeclaredField(name);
                f.setAccessible(true);
                f.set(this, obj);
            } catch (NoSuchFieldException | SecurityException
                    | IllegalArgumentException | IllegalAccessException e) {
                e.printStackTrace();
            }
            return this;
        }
    
    }
    View Code

    在name上加了  @NotBlank(groups= {First.class,Second.class}) 

    在address上加了 @NotBlank(groups= {Second.class}) 

    可以猜测当遇到 First标志时会验证name,在遇到Second标志时会验证name和address。 那么现在只要告诉spring验证时使用First还是Second标志就可以了。

    在Controller中有方法

        @RequestMapping(value="/add")
        public String add(@Validated(First.class) People p,BindingResult result, HttpServletRequest request) {
            if(result.hasErrors()) {
                System.out.println(result.getFieldErrors());
                return "error";
            }
            this.peopleService.add(p);
            return "people/detail";
        }    
        
     
        @RequestMapping("/update")
        public String update(@Validated(Second.class) People p,BindingResult result,HttpServletRequest request) {
            if(result.hasErrors()) {
                System.out.println(result.getFieldErrors());
                return "error";
            }
            this.peopleService.update(p);
            return "people/detail";
        }
    View Code

    在每个方法参数People p之前,把原来的@Valid改为了@Validated,并且分别加上了参数First.class和Second.class。

    初步发觉@Valid和@Validated不能混用。也就是说如果在field上加了 groups={xxx.class},那么就没法用@Valid了,必须用@Validated

  • 相关阅读:
    centos7下更新firefox
    Centos7宽带连接
    CAS和AQS
    java中锁的概念
    并发队列
    Callable和Future
    juc下的并发工具类和线程池
    死锁和线程安全的问题
    HashMap源码分析(java1.8)
    List集合框架面试题
  • 原文地址:https://www.cnblogs.com/formyjava/p/4589257.html
Copyright © 2011-2022 走看看