我從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