Spring框架的validator验证器的实现

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...

}

怎么样?还算简单吧,呵呵,欢迎指点或拍砖讨论... 懒人计划 大笑... 



  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值