java – Spring MVC跨域验证:代码气味?

前端之家收集整理的这篇文章主要介绍了java – Spring MVC跨域验证:代码气味?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这是一个代码的味道,还是在 Spring窗体中实现跨场验证的最佳方法
@FieldrequiredIf.List({
    @FieldrequiredIf(ifField="firstHomePhoneNumber",matches={EMPTY,NULL},require ="firstMobilePhoneNumber",elseDisplay = "FieldrequiredIf.correspondenceDetailsForm.firstMobilePhoneNumber",groups=FirstLife.class),@FieldrequiredIf(ifField="secondHomePhoneNumber",require ="secondMobilePhoneNumber",elseDisplay = "FieldrequiredIf.correspondenceDetailsForm.secondMobilePhoneNumber",groups=SecondLife.class),@FieldrequiredIf(ifField="lifeAssuredIsPolicyOwner",matches={FALSE},require ="policyOwnerName",elseDisplay = "FieldrequiredIf.correspondenceDetailsForm.policyOwnerName"),require ="policyOwnerAddress",elseDisplay = "FieldrequiredIf.correspondenceDetailsForm.policyOwnerAddress"),@FieldrequiredIf(ifField="insurableInterest",matches={InsurableInterestConstants.OTHER},require ="insurableInterestReason",elseDisplay = "FieldrequiredIf.correspondenceDetailsForm.insurableInterestReason",groups = NonSingleNonMortgage.class),@FieldrequiredIf(ifField="firstAddress2",matches={NOT_EMPTY},require ="firstAddress1",elseDisplay = "FieldrequiredIf.correspondenceDetailsForm.firstAddress1",@FieldrequiredIf(ifField="firstAddress3",require ="firstAddress2",elseDisplay = "FieldrequiredIf.correspondenceDetailsForm.firstAddress2",@FieldrequiredIf(ifField="firstAddress4",require ="firstAddress3",elseDisplay = "FieldrequiredIf.correspondenceDetailsForm.firstAddress3",@FieldrequiredIf(ifField="firstAddress5",require ="firstAddress4",elseDisplay = "FieldrequiredIf.correspondenceDetailsForm.firstAddress4",@FieldrequiredIf(ifField="secondAddress2",require ="secondAddress1",elseDisplay = "FieldrequiredIf.correspondenceDetailsForm.secondAddress1",@FieldrequiredIf(ifField="secondAddress3",require ="secondAddress2",elseDisplay = "FieldrequiredIf.correspondenceDetailsForm.secondAddress2",@FieldrequiredIf(ifField="secondAddress4",require ="secondAddress3",elseDisplay = "FieldrequiredIf.correspondenceDetailsForm.secondAddress3",@FieldrequiredIf(ifField="secondAddress5",require ="secondAddress4",elseDisplay = "FieldrequiredIf.correspondenceDetailsForm.secondAddress4",groups=SecondLife.class)
})
public class CorrespondenceDetailsForm {
    ...
}

重构

为了简化上述,我开始将这些列出的注释的几个逻辑组重构为单个自定义注释:(@FirstLifeContactDetailsObserver和@SecondLifeContactDetailsObserver).这是重构的类级别注释:

@FieldrequiredIf.List({
    @FieldrequiredIf(ifField="lifeAssuredIsPolicyOwner",groups = NonSingleNonMortgage.class)
})
@FirstLifeContactDetailsObserver
@SecondLifeContactDetailsObserver
public class CorrespondenceDetailsForm {
    ...
}

这并没有真正改善的情况,因为我需要一个验证器类为每个新的注释.

Spring MVC中是否有更好的跨域验证方法

解决方法

只是为了完成这个问题,并把它绑在一个漂亮的小弓,两个评论者在勘误中提到,这个场景的前图片实际上是 the recommended way to implement cross-field validations.

虽然它肯定是从Spring MVC的向上方向发出的设计气味,但这并不是您的代码的问题.

猜你在找的Java相关文章