[英]JSF2 custom UIComponent attributes data types
如何為UIComponent屬性創建自定義數據類型?
示例:假設有一個UIInputDate
(一個UIInput
)並具有一個屬性Date maxDate
,我如何確保輸入的任何maxDate
總是作為Date
?
您可以按照常規方式創建自定義驗證器。 輸入組件已經可以作為第二個參數使用,您只需要強制轉換即可。
public class UIInputDateRangeValidator implements Validator {
public void validate(FacesContext context, UIComponent component, Object value) {
UIInputDate inputDate = (UIInputDate) component;
Date minDate = inputDate.getMinDate();
Date maxDate = inputDate.getMaxDate();
Date date = (Date) value;
// ... Use Date#after(), Date#before(), etc.
}
}
您可以在自定義組件的構造函數中創建並添加驗證器。
public UIInputDate() {
addValidator(new UIInputDateRangeValidator());
// You can use setConverter() with new DateTimeConverter() if you didn't already do that.
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.