springmvc+hibernate validator对前端数据校验

hibernate-validator-5.1.2.Final用到的包和bean:
package com.action;

import javax.validation.constraints.Size;

import org.hibernate.validator.constraints.Email;






public class UserInfo {

	@Size(min=3,max=8,message="用户名长度为3到8位")
	private String name;
	@Size(min=11,max=11,message="电话号码只能为11位")
	private String phone;
	@Email(message="邮箱格式不正确")
	private String email;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	@Override
	public String toString() {
		return "UserInfo [name=" + name + ", phone=" + phone + ", email="
				+ email + "]";
	}
	
	
}

web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>springmvcday02_annotation</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>  
  </welcome-file-list>
  
  <!-- 配置springmvc核心控制器 -->
  <servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- 设置初始化优先级,数字越小越高,会在部署到tomcat时就被初始化创建 -->
		<load-on-startup>1</load-on-startup>
  </servlet>
  <!-- 配置springmvc入口 -->
  <servlet-mapping>
  		<servlet-name>springmvc</servlet-name>
  		<url-pattern>*.do</url-pattern>
  </servlet-mapping>
  
  <!-- 配置过滤器
  	        乱码问题处理:1.在web.xml文件中配置过滤器(post方式解决)
  	        	2.在tomcat的server.xml文件中的contoter节点下进行配置(get方式提交解决)
   -->
  <filter>
  		<filter-name>EncodingFilter</filter-name>
  		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  		<init-param>
  			<!-- 配置编码格式为utf-8 -->
  			<param-name>encoding</param-name>
  			<param-value>utf-8</param-value>
  		</init-param>
  		<!-- 配置是否强制使用过滤器 -->
  		<init-param>
  			<param-name>forceEncoding</param-name>
  			<param-value>true</param-value>
  		</init-param>
  </filter>
  <!-- 配置过滤器过滤地址 -->
  <filter-mapping>
  		<filter-name>EncodingFilter</filter-name>
  		<url-pattern>/*</url-pattern>
  </filter-mapping>
 
</web-app>

springmvc-servlet.xml配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">


	<!-- 加载推荐使用的驱动 -->
	<mvc:annotation-driven/>
	<!-- 开启注解扫描包 -->
	<context:component-scan base-package="com.action"></context:component-scan>
	
	<!-- HandlerMapping和HandlerAdaptaher在服务被部署时就已经被创建 -->
	<!-- 配置视图解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 配置视图解析路径 -->
		<!-- prefix="/WEB-INF/jsp/", suffix=".jsp", viewname="test" -> "/WEB-INF/jsp/test.jsp"  -->
		<property name="prefix" value="/WEB-INF/views/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
	
	<!-- 配置文件上传功能 -->
	<bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver" id="multipartResolver">
		<!-- 配置编码格式 -->
		<property name="DefaultEncoding" value="utf-8"></property>
		<!-- 配置文件上传大小:5M(自定义) 1024*1024*5 -->
		<property name="maxUploadSize" value="5242880"></property>
	</bean>
	
	<!-- 配置拦截器 -->
	<mvc:interceptors>
		<mvc:interceptor>
			<!-- 配置拦截器要拦截的路径 ,/**拦截所有目录-->
			<mvc:mapping path="/**"/>
			<!-- 配置自定义拦截器 -->
			<bean class="com.action.MyHandlerInterceptor"></bean>
		</mvc:interceptor>
	</mvc:interceptors>
	


	
</beans>

简单测试controller:

@RequestMapping(value="/hello19",method=RequestMethod.POST)
	public String hello19(@Validated UserInfo info,BindingResult  result){
		//对错误信息的收集处理
		StringBuffer sb=new StringBuffer();
		if(result.hasErrors()){
			List<ObjectError> errors=result.getAllErrors();
			for(ObjectError error:errors){
				sb.append(error.getDefaultMessage());
			}
			System.out.println("error:==="+sb.toString());
		}
		
		System.out.println("userInfo==="+info.toString());
		return null;
	}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值