我从JSF 2.0的某个地方读到,验证器例程需要正确处理value参数为null的情况。于是我就写了这个validate方法,看起来像这样:我真的需要在验证器中检查null吗?
public void validateTandC(FacesContext context, UIComponent source, Object value) {
if (value == null || !(value instanceof Boolean)) {
FacesMessage fm = new FacesMessage(FacesMessage.SEVERITY_ERROR,
"Terms and Conditions Not Accepted", "Value Not Present");
throw new ValidatorException(fm);
}
if (((Boolean) value) != true) {
FacesMessage fm = new FacesMessage(FacesMessage.SEVERITY_ERROR,
"Terms and Condistions Not Accepted",
"You must check the box that confirms you accept the Terms and Condtions ");
throw new ValidatorException(fm);
}
}
这是一个相当繁琐建设,因为在相关的页面,仅从单个h该方法仅简称:selectBooleanCheckbox。如果是这样的话,为什么第一个if声明永远解决为真?如果我没有,NPE会在现实生活中发生吗?
相关:[验证必填复选框](http://balusc.blogspot.com/2008/09/validate-required-checkbox.html)。 – BalusC 2012-01-02 13:35:55