0

我有 bean AddressBean,它具有一些属性,如 addressLine1、city 等。在使用 AddressBeanService 将其持久化到 DB 之前,我正在对其进行预验证,并在保存后,使用 ValidateAddressService 中的 preValidate 和 postValidate 函数对其进行验证。而这一切都是我从 AddressBeanHelper 类触发的。

class AddressBeanHelper{

    AddressBean bean =null;
    AddressBeanHelper(AddressBean bean){
        this.bean=bean;
    }

ValidationService validate=new ValidateAddressService();
    function doStuff(){
        validate.preValidateAddressBean (bean);
        //business logic for AddressBean
        validate.preValidateAddressBean (bean);
    }
}

class ValidateAddressService implements ValidationService <AddressBean>{
    preValidateAddressBean (AddressBean bean){
        //here is the issue
    }
    preValidateAddressBean (AddressBean bean){
        //here is the issue
    }
}

我想在 spring 中使用一些框架或技巧,我只需要在验证函数中编写通用代码并将我的验证规则外部化到代码本身之外。就像一个规则引擎,它可以自动验证 bean 的每一个属性。目前我的应用程序基础设施是服务器端的spring/hibernate,客户端的jsp/jquery和部署服务器在heroku上。

4

1 回答 1

1

看看这个:

http://hillert.blogspot.com/2011/12/method-validation-with-hibernate.html

它支持 JSR-303,因此它是一个标准。它非常容易实现,并且支持自定义和一些预定义的即用型验证器。

您还可以在此处找到一些参考资料:

http://java.dzone.com/articles/method-validation-spring-31

于 2012-05-18T09:39:34.520 回答