Spring4下validation数据校验⽆效(maven)的解决SpringMVC4使⽤数据校验的时候需要使⽤hibernate-validator的包
第⼀步添加依赖
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.4.1.Final</version>
</dependency>
第⼆步添加校验注解
package del;
/*
* spring4 ⽤hibernate中的validation
*
* */
import org.straints.Email;
import org.straints.NotBlank;
import straints.Size;
public class User {
private String username;
private String password;
private String nickname;
private String email;
public User(){
}
public User(String username, String password, String nickname, String email) {
this.username = username;
this.password = password;
this.nickname = nickname;
}
@NotBlank(message = "⽤户名不能为空")
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Size(min = 6,max = 20,message = "密码为6-20位")
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
@Email(message = "请输⼊合法的邮箱")
public String getEmail() {
return email;
}
public void setEmail(String email) {
}
}
第三步在控制器中对指定参数进⾏校验注解
注:BindingResult 必须紧跟在@Validate
//@Validated指定验证对象 BindingResult紧跟Validated
@RequestMapping(value="/add",method = RequestMethod.POST)
public String add(@Validated User user, BindingResult br){
System.out.println(br);
System.out.ErrorCount());
if(br.hasErrors())
return "user/add";
users.Username(),user);
return "redirect:/user/users";
}
注:这个配置也会影响到校验是否⽣效
<mvc:annotation-driven/>
SpringBoot-----数据验证validation(实战中,不推荐使⽤) 1、定义错误信息
在resources/下创建名称为ValidationMessages.properties
=⽤户名不允许为空
=⽤户名的注册必须输⼊正确的邮箱
member.=⽤户名的格式错误
2、给Vo类添加注解
l.micorboot.vo;
import java.io.Serializable;
import java.util.Date;
import straints.Email;
import straints.NotNull;
import org.straints.Length;
public class Member implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;springframework依赖
@NotNull(message="{}")
@Email(message="{}")
@Length(min=6,message="{member.}")
private String mid;
private Integer age;
private Double salary;
private Date birthday;
public String getMid() {
return mid;
}
public void setMid(String mid) {
this.mid = mid;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
3、控制添加验证
@Valid Member vo, BindingResult result
ller;
import java.util.List;
import javax.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
l.micorboot.base.AbstractBaseController;
l.micorboot.vo.Member;
@Controller
public class MemberController extends AbstractBaseController {
@GetMapping("/addPre")
public String addPre(){
return "message/member_add";
}
@GetMapping("/add")
@ResponseBody
public Object add(@Valid Member vo, BindingResult result){
System.out.println("【ADD⽅法】"+result.hasErrors());
if(result.hasErrors()){
List<ObjectError> list = AllErrors();
list.forEach(oe -> {
});
AllErrors();
}
return vo;
}
}
4、访问结果
[{"codes":["ber.mid","Email.mid","Email.java.lang.String","Email"],"arguments":[{"codes":
["member.mid","mid"],"arguments":null,"defaultMessage":"mid","code":"mid"},[],
{"defaultMessage":".*","arguments":null,"codes":[".*"]}],"defaultMessage":"ç¨æ·åç注åå¿é¡»è¾å¥æ‐
£ç¡®çé®ç®±","objectName":"member","field":"mid","rejectedValue":"xx","bindingFailure":false,"code":"Email"}]以上为个⼈经验,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论