java - JSF2 自定义 UIComponent 属性数据类型

标签 java jsf-2

如何为 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/

相关文章:

java - 我想以编程方式向我的 android 按钮添加多个字符串(不是通过 XML)

java - 如何移动颜色中的位(十六进制值)

jsf - 错误解析/page.xhtml : Error Traced[line: 42] The entity "nbsp" was referenced, 但未声明

css - 管理 JSF 中资源加载的顺序

JSF 2.0 request.getParameter 返回编码错误的字符串

JAVA : Reading txt and take elements with StringTokenizer

java - 将数组转换为 ArrayList 的通用方法

java - ActionListener 不适用于所有对象

jsf - faces-redirect和返回按钮导致其他链接无法正常工作

jsf - p :pickList doesn't update the source and target