【Java】RuleSource约束常用方法整理
生活随笔
收集整理的這篇文章主要介紹了
【Java】RuleSource约束常用方法整理
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1-常用約束規(guī)則RuleSource的設(shè)置方法 例如: addRules(new Rules(ProgramFeeItem.class){ protected void initRules() { add("rateClass", all(new Constraint[] { required() }));??//required表示,不可為空 add("remark", rules.maxLength(250)); ????//maxLength約束最大長度250 addMoneyRule(this,"payingSumFee");? ?? ????????} });
2-自定義約束規(guī)則方法: 創(chuàng)建AbstractPropertyContraint對象,并實現(xiàn)test和toString方法,test方法用戶定義約束規(guī)則 ,toString方法用戶設(shè)置異常信息 例如: addRules(new Rules(ProgramFeeItem.class){ protected void initRules() { addMoneyRule(this,"payingSumFee");? ??//addMoneyRule自定義約束規(guī)則 ????????} }); protected void addMoneyRule(Rules rules,final String datePath){ rules.add(new AbstractPropertyConstraint(datePath) { @Override protected boolean test(??//規(guī)則檢測方法 PropertyAccessStrategy domainObjectAccessStrategy) { ????????????????????????????????
//通過策略獲取payingSumFee的值 Money amount = (Money) domainObjectAccessStrategy.getPropertyValue(datePath); //自定義規(guī)則Money的范圍 return amount.gte(new Money(-9999999999.99D))&&amount.lte(new Money(9999999999.99D)); } ? @Override public String toString() {//用戶設(shè)置異常返回碼 return Application.services().getApplicationContext() .getMessage("gte.-9999999999.99.and.lte.9999999999.99", new Object[] {}, "gte.-9999999999.99.and.lte.9999999999.99", Locale.getDefault()); } }); }
3-兩控件聯(lián)動約束規(guī)則設(shè)置方法: 使用RequireAIfBTrue對象實現(xiàn),? public RequireAIfBTrue(String a, String b, Constraint aConstraint, Constraint bContraint,String msgKey) { 其中 a 要求約束對應(yīng)的屬性 b 要求約束的前提約束對應(yīng)的屬性 aConstraint Constraint 要求約束 bContraint Constraint 要求約束的前提約束 msgKey String 國際化信息 例如: addRules("receipt",new Rules(ProgramWorkOrder.class) { protected void initRules() { add(new RequireAIfBTrue("failureReason", "taskResult", required(), new Constraint() {????//指定約束規(guī)則 public boolean test(Object argument) {?????//約束規(guī)則設(shè)置 TaskResult result = (TaskResult) argument; if (result != null && result.ordinal() == TaskResult.UNSUCCESS.ordinal()) { return true; } return false; } }, "required")); ????//required國際化信息 } }); ?
轉(zhuǎn)載于:https://www.cnblogs.com/liuyongcn/p/3553322.html
總結(jié)
以上是生活随笔為你收集整理的【Java】RuleSource约束常用方法整理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Haproxy安装及配置(转)
- 下一篇: Java运行原理研究(未完待续)