一、导入Hibernate Validator的相关Jar包
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/18f1943b9f31c47ddbc00c7338991c39.png)
二、环境搭建
1.配置web.xml
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:demo1/springMVC.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:demo1/springMVC.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<param-name>forceResponseEncoding</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>forceRequestEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2.配置springMVC.xml
<context:component-scan base-package="demo1"></context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
3.前端页面
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/aeec2f2fffe2d4ffcb4796db0a77ace6.png)
4.实体类
public class Person {
@NotEmpty
private String username;
@Length(min = 5,max = 8)
private String password;
@Email
private String email;
}
5.控制层
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/97e0815864c88b81068df703435c2286.png)
6.结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/482948065028007788627faef2815d84.png)