1.基本介绍
之前在项目中做的后台验证就是Spring Validation,最近闲下来了,就来整理一下。
从Spring3.0开始,Spring MVC中提供了对java校验的API支持。在Spring MVC中不需要额外配置,就可以使用。
注解 | 描述 |
@AssertFalse | 所注解的元素必须为boolean类型,并且值为false |
@AssertTrue | 所注解的元素必须为boolean类型,并且值为true |
@DecimalMax | 所注解的元素必须为数字,并且值要小于或等于给定的BigDecimalString值 |
@DecimalMin | 所注解的元素必须为数字,并且值要大于或等于给定的BigDecimalString值 |
@Digits | 所注解的元素必须为数字,并且它的值必须有指定的位数 |
@Future | 所注解的元素必须为一个将来的日期 |
@Max | 所注解的元素必须为数字,并且值要小于或等于给定的值 |
@Min | 所注解的元素必须为数字,并且值要大于或等于给定的值 |
@NotNull | 所注解的元素不能为null |
@Null | 所注解的元素必须为null |
@Past | 所注解的元素的值必须是一个已过去的日期 |
@Pattern | 所注解的元素必须匹配给定的正则表达式 |
@Size | 所注解的元素必须是String、数组、集合,并且长度在给定范围内 |
2.代码展示
user.java
1 /** 2 * TODO 用户信息PO <br/> 3 * TODO 用户信息类 4 * @author tzy 5 */ 6 @Entity 7 @Table(name = "userInfo") 8 public class UserInfoPO implements Serializable{ 9 10 /** 11 * 12 */ 13 private static final long serialVersionUID = 1L; 14 /** 15 * 人员id 16 */ 17 @Id 18 @GenericGenerator(name = "systemUUID", strategy = "uuid") 19 @GeneratedValue(generator = "systemUUID") 20 @Column(name = "userInfoId", length = 32) 21 private String userInfoId; 22 /** 23 * 用户id 24 */ 25 @OneToOne(cascade = { CascadeType.REFRESH }, fetch=FetchType.LAZY) 26 @JoinColumn(name = "userId") 27 private UserPO user; 28 /** 29 * 姓名 30 */ 31 @NotBlank(message = "联系人为空") 32 @Column(name = "name", length = 20) 33 private String name; 34 /** 35 * 性别 1.男 2.女 36 */ 37 @Column(name = "sex", length = 1) 38 private Integer sex; 39 /** 40 * 学历 41 */ 42 @Column(name = "qualification", length = 20) 43 private String qualification; 44 /** 45 * 出生日期 46 */ 47 @Temporal(TemporalType.DATE) 48 @Column(name = "birthDay") 49 private Date birthDay; 50 /** 51 * 身份证 52 */ 53 @Column(name = "idCard", length = 19) 54 private String idCard; 55 /** 56 * 固话 57 */ 58 @Column(name = "tel", length = 12) 59 private String tel; 60 /** 61 * 手机号 62 */ 63 @NotBlank(message = "手机号为空") 64 @Size(min = 11, max = 11, message = "手机号长度有误") 65 @Column(name = "phone", length = 12) 66 private String phone; 67 /** 68 * 邮箱 69 */ 70 @NotBlank(message = "邮箱为空") 71 @Email(message = "用户名格式有误") 72 @Column(name = "email", length = 50) 73 private String email; 74 /** 75 * 图片 76 */ 77 @Column(name = "image", length = 255) 78 private String image; 79 /** 80 * 参加工作时间 81 */ 82 @Temporal(TemporalType.DATE) 83 @Column(name = "joinWork") 84 private Date joinWork; 85 /** 86 * 入职时间 87 */ 88 @Temporal(TemporalType.DATE) 89 @Column(name = "hireDate") 90 private Date hireDate; 91 /** 92 * 工作经历 93 */ 94 @Column(name = "wordDes", length = 400) 95 private String wordDes; 96 /** 97 * 地址 98 */ 99 @Column(name = "address", length = 100) 100 private String address; 101 /** 102 * 备注 103 */ 104 @Column(name = "comment", length = 255) 105 private String comment; 106 /** 107 * 机构名称 108 */ 109 @NotBlank(message = "用户名称为空") 110 @Column(name = "orgName", length = 100) 111 private String orgName; 112 /** 113 * 传真 114 */ 115 @Column(name = "fax", length = 20) 116 private String fax; 117 118 public String getUserInfoId() { 119 return userInfoId; 120 } 121 122 public void setUserInfoId(String userInfoId) { 123 this.userInfoId = userInfoId; 124 } 125 126 public UserPO getUser() { 127 return user; 128 } 129 130 public void setUser(UserPO user) { 131 this.user = user; 132 } 133 134 public String getName() { 135 return name; 136 } 137 138 public void setName(String name) { 139 this.name = name; 140 } 141 142 public Integer getSex() { 143 return sex; 144 } 145 146 public void setSex(Integer sex) { 147 this.sex = sex; 148 } 149 150 public String getQualification() { 151 return qualification; 152 } 153 154 public void setQualification(String qualification) { 155 this.qualification = qualification; 156 } 157 158 public Date getBirthDay() { 159 return birthDay; 160 } 161 162 public void setBirthDay(Date birthDay) { 163 this.birthDay = birthDay; 164 } 165 166 public String getIdCard() { 167 return idCard; 168 } 169 170 public void setIdCard(String idCard) { 171 this.idCard = idCard; 172 } 173 174 public String getTel() { 175 return tel; 176 } 177 178 public void setTel(String tel) { 179 this.tel = tel; 180 } 181 182 public String getPhone() { 183 return phone; 184 } 185 186 public void setPhone(String phone) { 187 this.phone = phone; 188 } 189 190 public String getEmail() { 191 return email; 192 } 193 194 public void setEmail(String email) { 195 this.email = email; 196 } 197 198 public String getImage() { 199 return image; 200 } 201 202 public void setImage(String image) { 203 this.image = image; 204 } 205 206 public Date getJoinWork() { 207 return joinWork; 208 } 209 210 public void setJoinWork(Date joinWork) { 211 this.joinWork = joinWork; 212 } 213 214 public Date getHireDate() { 215 return hireDate; 216 } 217 218 public void setHireDate(Date hireDate) { 219 this.hireDate = hireDate; 220 } 221 222 public String getWordDes() { 223 return wordDes; 224 } 225 226 public void setWordDes(String wordDes) { 227 this.wordDes = wordDes; 228 } 229 230 public String getComment() { 231 return comment; 232 } 233 234 public void setComment(String comment) { 235 this.comment = comment; 236 } 237 238 public String getAddress() { 239 return address; 240 } 241 242 public void setAddress(String address) { 243 this.address = address; 244 } 245 246 public String getOrgName() { 247 return orgName; 248 } 249 250 public void setOrgName(String orgName) { 251 this.orgName = orgName; 252 } 253 254 public String getFax() { 255 return fax; 256 } 257 258 public void setFax(String fax) { 259 this.fax = fax; 260 } 261 262 }
...controller
1 @RequestMapping("/toUserInfoModifyDeal") 2 @UserLog(type = "修改用户信息", level = "1", desc = "修改用户信息") 3 public @ResponseBody String toUserInfoModifyDeal( 4 @Valid UserInfoPO userInfoPO, BindingResult result) { 5 if (result.hasErrors()) { 6 List<ObjectError> list = result.getAllErrors(); 7 for (int i1 = 0; i1 < list.size(); i1++) { 8 } 9 return "用户信息修改失败!"; 10 } 11 ...... 12 }