————吾亦无他,唯手熟尔,谦卑若愚,好学若饥————-
校验有三种:前台页面校验(例如js和h5),后台校验,数据库校验
但是一般能不用数据库校验就不用数据库校验,因为性能损耗严重,
所以,我们用前台和后台俩种,可以说最好,绝大部分情况下,前台后台都要写,如果一个用户比较搞事,直接把它浏览器上的js给禁用掉了,那么如果没有后台校验,他将直接杀向数据库
很多人一提hibernate就说,哦,是一个框架,ORM框架,数据库访问层的框架,SSH的那个H!!!!
这个答案其实很一般,HibernateORM是Hibernate框架其中的一个重要的组成部分之一,像其它的例如hibernate-validator这个校验框架其实很不错
好了,不扯皮了,说说怎么用。。。。。。。
案例:
一,引入jar包
1
2
3
4
5
6
7
8
9
10
11
12
13 1 <dependency>
2 <groupId>org.hibernate</groupId>
3 <artifactId>hibernate-validator</artifactId>
4 <version>4.3.1.Final</version>
5 </dependency>
6
7 <!--validation api-->
8 <dependency>
9 <groupId>javax.validation</groupId>
10 <artifactId>validation-api</artifactId>
11 <version>1.0.0.GA</version>
12 </dependency>
13
也不知道大家看到了没有,由于是maven项目,只需要俩个节点就好,它会自动引入相关联的jar,如果不是maven,或者maven不好用,则还需要再引俩个jar,我把全部jar包的节点放上来
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 1 <!--数据验证-->
2 <dependency>
3 <groupId>org.hibernate</groupId>
4 <artifactId>hibernate-validator</artifactId>
5 <version>4.0.1.GA</version>
6 </dependency>
7
8 <!--jboss logging-->
9 <dependency>
10 <groupId>org.jboss.logging</groupId>
11 <artifactId>jboss-logging</artifactId>
12 <version>3.3.0.Final</version>
13 </dependency>
14
15 <!--validation api-->
16 <dependency>
17 <groupId>javax.validation</groupId>
18 <artifactId>validation-api</artifactId>
19 <version>1.0.0.GA</version>
20 </dependency>
21
22 <!--slf4j api-->
23 <dependency>
24 <groupId>org.slf4j</groupId>
25 <artifactId>slf4j-api</artifactId>
26 <version>1.7.21</version>
27 </dependency>
28
数据校验全部的jar,合起来
二,自己的大配置文件xml,里面需要新配置一个验证器,并且绑定注解驱动
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 1<?xml version="1.0" encoding="UTF-8"?>
2<beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:mvc="http://www.springframework.org/schema/mvc"
5 xmlns:context="http://www.springframework.org/schema/context"
6 xsi:schemaLocation="http://www.springframework.org/schema/beans
7 http://www.springframework.org/schema/beans/spring-beans.xsd
8 http://www.springframework.org/schema/mvc
9 http://www.springframework.org/schema/mvc/spring-mvc.xsd
10 http://www.springframework.org/schema/context
11 http://www.springframework.org/schema/context/spring-context.xsd">
12
13 <!--包扫描器-->
14 <context:component-scan base-package="cn.dawn.day23validator"></context:component-scan>
15 <!--视图解析器-->
16 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
17 <property name="prefix" value="/day23/"></property>
18 <property name="suffix" value=".jsp"></property>
19 </bean>
20
21 <!--验证器-->
22 <bean id="myvalidator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
23 <!--注入hibernate的验证器-->
24 <property name="providerClass" value="org.hibernate.validator.HibernateValidator"></property>
25 </bean>
26
27 <!--绑定注解驱动-->
28 <mvc:annotation-driven validator="myvalidator"></mvc:annotation-driven>
29
30</beans>
31
三,修改web.xml中央调度器的上下文配置位置为上面那个配置文件
四,建一个实体类:里面用注解做校验,很好理解,有些用正则
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64 1package cn.dawn.day23validator;
2
3import org.hibernate.validator.constraints.NotEmpty;
4import org.springframework.format.annotation.DateTimeFormat;
5
6
7import javax.validation.constraints.Max;
8import javax.validation.constraints.Min;
9import javax.validation.constraints.Pattern;
10import javax.validation.constraints.Size;
11import java.util.Date;
12
13/**
14 * Created by Dawn on 2018/3/31.
15 */
16public class UserInfo {
17 @NotEmpty(message = "用户名不能为空")
18 @Size(min = 6,max = 20,message = "用户名的长度必须在{min}-{max}之间")
19 private String username;
20
21 @Max(value = 150,message = "最老150岁")
22 @Min(value = 0,message = "最小0岁")
23 private Integer userage;
24
25 @NotEmpty(message = "手机号码不能为空")
26 @Pattern(regexp = "^1[3|5|7|8|9]\\d{9}$",message = "手机号码格式不正确")
27 private String userphone;
28 @NotEmpty(message = "邮箱不能为空")
29 @Pattern(regexp = "^\\w+@\\w+\\.\\w+$",message = "邮箱格式不正确")
30 private String email;
31
32 public String getUsername() {
33 return username;
34 }
35
36 public void setUsername(String username) {
37 this.username = username;
38 }
39
40 public Integer getUserage() {
41 return userage;
42 }
43
44 public void setUserage(Integer userage) {
45 this.userage = userage;
46 }
47
48 public String getUserphone() {
49 return userphone;
50 }
51
52 public void setUserphone(String userphone) {
53 this.userphone = userphone;
54 }
55
56 public String getEmail() {
57 return email;
58 }
59
60 public void setEmail(String email) {
61 this.email = email;
62 }
63}
64
五,自定义的处理器和处理方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 1package cn.dawn.day23validator;
2
3import org.springframework.stereotype.Controller;
4import org.springframework.ui.Model;
5import org.springframework.validation.BindingResult;
6import org.springframework.validation.FieldError;
7import org.springframework.web.bind.annotation.RequestMapping;
8import org.springframework.web.servlet.ModelAndView;
9
10import javax.validation.Valid;
11
12/**
13 * Created by Dawn on 2018/3/31.
14 */
15@Controller
16public class ValidatorController {
17 @RequestMapping("/validatorZC")
18 public String validatorZC(@Valid UserInfo userInfo, BindingResult br, Model model){
19 //判定br中错误数是否大于0,如果true,证明有一个验证失败了
20 if (br.getErrorCount()>0){
21 FieldError userage=br.getFieldError("userage");
22 FieldError username=br.getFieldError("username");
23 FieldError userphone=br.getFieldError("userphone");
24 FieldError email=br.getFieldError("email");
25
26 //哪个验证失败,就回显示哪个
27 if(userage!=null){
28 String useragemsg = userage.getDefaultMessage();
29 model.addAttribute("useragemsg",useragemsg);
30 }
31 if(username!=null){
32 String usernamemsg = username.getDefaultMessage();
33 model.addAttribute("usernamemsg",usernamemsg);
34 }
35 if(userphone!=null){
36 String userphonemsg = userphone.getDefaultMessage();
37 model.addAttribute("userphonemsg",userphonemsg);
38 }
39 if(email!=null){
40 String emailmsg = email.getDefaultMessage();
41 model.addAttribute("emailmsg",emailmsg);
42 }
43
44 //这个if里面就是肯定有错误,回显示名字
45 model.addAttribute("username",userInfo.getUsername());
46 /*返回注册页面*/
47 return "validator";
48 }
49 /*注册成功*/
50 return "success";
51
52 }
53}
54
六,jsp页面
6.1validator.jsp页面
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 1<%--
2 Created by IntelliJ IDEA.
3 User: Dawn
4 Date: 2018/3/31
5 Time: 17:21
6 To change this template use File | Settings | File Templates.
7--%>
8<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
9<html>
10<head>
11 <title>Title</title>
12</head>
13<body>
14
15<h1>数据校验</h1>
16<form action="${pageContext.request.contextPath}/validatorZC" method="post">
17 年龄:<input name="userage" /> <span>${useragemsg }</span><br/><br/>
18 姓名:<input name="username" value="${username}"/><span>${usernamemsg }</span><br/><br/>
19 电话:<input name="userphone"/><span>${userphonemsg }</span><br/><br/>
20 邮箱:<input name="email"/> <span>${emailmsg}</span><br/><br/>
21 <input type="submit" value="注册"/>
22</form>
23</body>
24</html>
25
6.2success.jsp页面
1
2
3
4
5
6
7
8 1<%@ page language="java" pageEncoding="utf-8" isELIgnored="false" %>
2<html>
3<body>
4<%--<img src="image/1.jpg">--%>
5<h2>Success!</h2>
6</body>
7</html>
8
七,启动tomcat,访问validator.jsp页面
有什么不懂的,联系我,原作者:晨曦Dawn
转载请注明出处 http://www.cnblogs.com/DawnCHENXI/p/8684167.html