[英]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.