Spring框架的 validator 组件,是个辅助组件,在进行数据的完整性和有效性非常有用,通过定义一个某个验证器,即可在其它需要的地方,使用即可,非常通用。
那验证器该如何定义,注入到spring容器中以及如何使用呢?下面分别说明一下:
一,验证器的定义,其实就是定义一个Java Bean并扩展了spring框架的 validator 接口,例如产品类表的验证器如下;
package com.xxx.validator;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import com.xxx.model.ProductCategory;
import com.xxx.service.ProductCategoryService;
@Component
public class ProductCategoryValidator implements Validator {
@Autowired
private ProductCategoryService<ProductCategory> productCategoryService;
@Override
public boolean supports(Class<?> claaz){
return ProductCategory.class.isAssignableFrom(claaz);
}
@Override
public void validate(Object object, Errors errors){
ProductCategory productCategory = (ProductCategory)object;
if(null == productCategory.getParentCategoryId() || productCategory.getParentCategoryId() < 0){
errors.rejectValue("parentCategoryId", "required", null,"产品父类别ID不能为空,且为正整数");
}
if(StringUtils.isBlank(productCategory.getProductCategoryName().trim())){
errors.rejectValue("productCategoryName", "required", null,"产品类别名称不能为空");
}
if(null == productCategory.getRecId()){
ProductCategory checkProductCategory = productCategoryService.getProductCategoryByProductCategoryName(productCategory.getProductCategoryName());
if(null != checkProductCategory){
errors.rejectValue("productCategoryName", "required", null,"该产品类别名称[ " + productCategory.getProductCategoryName() + " ]已经存在");
}
checkProductCategory = null;
}
}
}
二,如何注入到spring容器中,其实就是在spring mvc 的配置文件中,扫描验证器所在的包,同时在验证器的Java Bean类前面加上注解 @Component 即可;当然也可在spring的配置文件 applicationContent.xml 中用常规的配置方式加载该验证器到spring容器中;
<!-- 扫描验证器所在的包 -->
<context:component-scan base-package="com.xxx.validator"/>
@Component
public class ProductCategoryValidator implements Validator {
//... validator code...
}
<!-- 在spring的配置文件 applicationContent.xml 中用常规的配置方式加载该验证器 -->
<bean id="productCategoryValidator" class="com.xxx.validator.ProductCategoryValidator">
</bean>
三,如何使用呢?其实就是在某个controller中首先 @autowired 该验证器到当前controller中,然后在某方法(如保存方法)中调用验证器的 validate 方法,值得注意的是该validate方法的两个参数,前一个是接受client发送过来包含数据的某个model(如:productCategory),后一个是BindingResult类型变量,用于保存验证结果可能出现的错误信息,值得注意的是在方法的参数当中,BindingResult 参数变量须紧紧跟在 model 参数变量之后,如果中间间隔任意一个其它参数变量那就报错了,然后根据验证结果,根据有无验证信息,进行相应的操作,参考如下片段代码所示:
//某controller的部分代码
@Autowired
private ProductCategoryValidator productCategoryValidator;
@RequestMapping(value = "/xpath/saveProductCategory", method = RequestMethod.POST)
public void saveProductCategory(
ProductCategory productCategory,
BindingResult productCategoryValidateResult, //必须紧紧跟在productCategory之后
HttpServletRequest request,
HttpServletResponse response,
HttpSession session,
Model model) throws Throwable{
//...other code...
productCategoryValidator.validate(ProductCategory, productCategoryValidateResult);
if(productCategoryValidateResult.hasErrors()){
//validate fail go here...
}else{
//validate success go here...
}
//...other code...
}
怎么样?还算简单吧,呵呵,欢迎指点或拍砖讨论... 懒人计划 ...