如何为 UIComponent 属性创建自定义数据类型?
示例:假设一个人有一个 UIInputDate
(一个 UIInput
)并且有一个属性 Date maxDate
,我将如何确保无论maxDate
输入的结果始终为 Date
?
最佳答案
您可以按照通常的方式创建自定义 validator 。输入组件已经作为第二个参数可用,您只需对其进行转换。
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.
}
}
您可以在自定义组件的构造函数中创建和添加 validator 。
public UIInputDate() {
addValidator(new UIInputDateRangeValidator());
// You can use setConverter() with new DateTimeConverter() if you didn't already do that.
}
关于java - JSF2 自定义 UIComponent 属性数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18177155/