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
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值