这篇文章主要介绍Spring4下validation数据校验无效怎么办,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
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 com.demo.model; /* * spring4 用hibernate中的validation * * */ import org.hibernate.validator.constraints.Email; import org.hibernate.validator.constraints.NotBlank; import javax.validation.constraints.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; this.email = email; } @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) { this.email = 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.println(br.getErrorCount()); if(br.hasErrors()) return "user/add"; users.put(user.getUsername(),user); return "redirect:/user/users"; }
注:这个配置也会影响到校验是否生效
<mvc:annotation-driven/>
在resources/下创建名称为ValidationMessages.properties
member.mid.notnull.error=用户名不允许为空 member.mid.email.error=用户名的注册必须输入正确的邮箱 member.mid.length.error=用户名的格式错误
package com.zemel.micorboot.vo; import java.io.Serializable; import java.util.Date; import javax.validation.constraints.Email; import javax.validation.constraints.NotNull; import org.hibernate.validator.constraints.Length; public class Member implements Serializable{ /** * */ private static final long serialVersionUID = 1L; @NotNull(message="{member.mid.notnull.error}") @Email(message="{member.mid.email.error}") @Length(min=6,message="{member.mid.length.error}") 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; } }
@Valid Member vo, BindingResult result package com.zemel.micorboot.controller; 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; import com.zemel.micorboot.base.AbstractBaseController; import com.zemel.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 = result.getAllErrors(); list.forEach(oe -> { }); return result.getAllErrors(); } return vo; } }
[{"codes":["Email.member.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"}]
以上是“Spring4下validation数据校验无效怎么办”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。