我有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
}
}
我想在春天使用一些框架或技巧,我只需要在验证函数中编写通用代码,并在代码本身之外外化我的验证规则。 就像一个规则引擎,它可以自动验证bean的每个属性。 目前我的应用程序基础架构是服务器端的spring / hibernate,客户端的jsp / jquery和部署服务器在heroku上。
答案 0 :(得分:1)
看一下这个:
http://hillert.blogspot.com/2011/12/method-validation-with-hibernate.html
它支持JSR-303,因此它是一个标准。它非常易于实现,并且支持自定义和一些预定义的现成验证器。
您也可以在这里找到一些参考资料: