我在 javabean 中有以下代码:
@AssertTrue
private boolean addressReferenceValid;
public boolean isAddressReferenceValid() {
if (addressType.equals(AddressType.ON_THE_FLY_ADDRESS) && StringUtils.isEmpty(addressReference)) {
return false;
}
return true;
}
我遇到的问题是从未使用过isAddressReferenceValid访问器,而且似乎只使用了addressReferenceValid的默认值(即false
)。
我仔细检查了 Spring Roo 没有为该变量生成自己的访问器。
有人可以帮忙吗?
PS我不能将@AssertTrue 注释直接放在方法上,因为这样ValidationMessages.properties
文件中的以下键将无法解析:AssertTrue.familyAdvertisementSearchCriteriaInfo.addressReferenceValid
编辑1:
全豆:
@RooEquals
@RooJavaBean
public class FamilyAdvertisementSearchCriteriaInfo {
@Valid
private FamilyAdvertisementSearchCriteria searchCriteria;
private Address currentAddress;
private String addressReference;
@NotNull
private AddressType addressType;
@AssertTrue(groups=Default.class)
private boolean addressReferenceValid;
public boolean isAddressReferenceValid() {
if (addressType.equals(AddressType.ON_THE_FLY_ADDRESS) && StringUtils.isEmpty(addressReference)) {
return false;
}
return true;
}
}
验证发生在以下控制器中:
@RequestMapping(value = "/familyAdvertisementSearch", method = RequestMethod.POST, produces = "text/html")
public String familyAdvertisementSearchResults(@ModelAttribute @Validated(Default.class) FamilyAdvertisementSearchCriteriaInfo familyAdvertisementSearchCriteriaInfo, BindingResult bindingResult, Model model) {
if(bindingResult.hasErrors()){
populateModel(model);
familyAdvertisementSearchCriteriaInfo.setCurrentAddress(memberService.retrieveCurrentMemberAddress());
return "search/familyAdvertisementSearchForm";
}
...