[英]How to Create a bean validation framework using Spring
我正在使用bean AddressBean,它具有一些屬性,如addressLine1,city等。在使用AddressBeanService將其持久化到數據庫之前,我要對其進行預驗證,並在保存后使用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。
看看這個:
http://hillert.blogspot.com/2011/12/method-validation-with-hibernate.html
它支持JSR-303,因此是一個標准。 它非常容易實現,並且支持自定義和一些預定義的隨時可用的驗證器。
您還可以在這里找到一些參考:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.