服务端校验:检查客户端所提交的数据是否符合规范
最早的校验,就是服务器端校验。早期的网站,用户输入一个邮箱地址,校验邮箱地址需要将地址发送到服务端,服务端进行校验,校验成功后,给前端一个响应,有了JavaScript,校验工作可以放在前端去执行。那么为什么还需要服务端校验呢?因为前端传来的数据不可信。前端很容易获取到后端的接口数据,如果有人绕过页面,就会出现非法数据,所以服务端也需要数据校验,总的来说:1、前端校验要做,目的是为了提高用户体验。2、后端校验也要做,目的是为了数据安全
注意:SpringMVC本身没有校验功能,它使用hiberna的校验框架,hiberna的校验框架和orm没有关系
加入相关依赖:
在java对象中的头部指定每一个属性的验证规则
我们的中央控制器,到我们的处理器适配器,处理器适配器到controller,获取到成员变量,如果其中有一个book对象,它会对book中的属性做验证,会得到book中的成员变量,通过成员变量就会知道成员变量的验证规则,然后去进行验证
但是这种方式验证规则直接写死了,一般在开发中不会写死它,我们可以在src下面创建一个属性文件
LocalValidatorFactoryBean:这个是springframework中提供的,是spring框架提供的有一个扩展第三方验证框架的一个FactoryBean,在它里面有一个providerClass文件就是org.hibernate.validator.HibernateValidator,这行代码将我们本地所使用的hibernate的框架跟spring结合起来,整合到一块儿了。
validatemessageSource中配置的属性文件,不用加 .properties后缀
<?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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
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-4.3.xsd">
<!-- 开启扫描 -->
<context:component-scan
base-package="com.sxt.controller"></context:component-scan>
<!-- 开启springmvc注解的方式 -->
<mvc:annotation-driven validator="validator"></mvc:annotation-driven>
<!--添加对JSR-303验证框架的支持 -->
<bean id="validator"
class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="providerClass"
value="org.hibernate.validator.HibernateValidator" />
<!--不设置则默认为classpath下的 ValidationMessages.properties -->
<property name="validationMessageSource"
ref="validatemessageSource" />
</bean>
<bean id="validatemessageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:ValidateMessages" />
<property name="fileEncodings" value="utf-8" />
<property name="cacheSeconds" value="120" />
</bean>
</beans>
然后还要把validator加到注解方式中
这就是开启验证框架,刚刚所做的都是准备工作,在项目中如果要用hibernate验证框架做验证,需要把hibernate验证框架整合进来,配置它的属性文件,然后再去在你所需要的这些java对象的成员变量的头部,去加它的验证规则,这之后如果想在业务中去使用验证,那么接下来就是具体的验证操作了,前面的这些基本上只需要设置一次就可以了。
例如现在我希望在fun1()方法中对Book对象做验证,如果就现在这样它不会验证
@Controller
public class UserController {
@RequestMapping("/add")
public void fun1(Book book) {
System.out.println(book);
}
我们需要在Book的头部加注解@Validated,如果有多个对象要验证,那么就在每一个对象的前面加上一个@Validated注解,也就是容器将数据往Book里面去写的时候,如果加的有@Validated注解,它就会对这个对象中的数据按照我们在成员变量中所指定的规则做验证,验证完之后的结果会存在一个叫BindingResult的对象中
package com.sxt.controller;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.sxt.pojo.Book;
@Controller
public class UserController {
/**
* 如果我们需要对Book做验证
* 那么需要在Book前面加@Validated注解
* BindingResult 保存的有验证的结果信息 验证报告
* @param book
*/
@RequestMapping("/add")
@ResponseBody
public void fun1(@Validated Book book,BindingResult br) {
//获取所有的错误信息
List<ObjectError> errors=br.getAllErrors();
if (errors!=null&&errors.size()>0) {
//肯定有错误信息
for (ObjectError e : errors) {
System.out.println(e.getDefaultMessage());
}
}else {
//数据合格
System.out.println(book);
}
}
}
book.jsp
如果什么也不写,直接提交
注意:中文占3位