This question already has an answer here:
How to use java.time.ZonedDateTime / LocalDateTime in p:calendar

(1个答案)


5年前关闭。




JSF核心标签f:convertDateTime可以格式化java.util.Date对象。 Date类具有许多不赞成使用的方法,Java 8中引入了新类来表示本地日期和时间:LocalDateTimeLocalDate

f:convertDateTime不能设置LocalDateTimeLocalDate的格式。

有人知道,如果有一个等效于JSF核心标记的convertDateTime可以处理LocalDateTime对象?是否计划在将来的版本中提供支持,或者有可用的替代标签?

最佳答案

只需编写您自己的Converter并扩展javax.faces.convert.DateTimeConverter-这样,您就可以重用<f:convertDateTime>支持的所有属性。同样,它也会照顾本地化。不幸的是,编写带有属性的Converter有点复杂。

创建组件
首先编写您自己的扩展javax.faces.convert.DateTimeConverter的Converter-只需让 super 调用完成所有工作(包括locale-stuff),然后将结果从LocalDate转换为LocalDate。

@FacesConverter(value = LocalDateConverter.ID)
public class LocalDateConverter extends DateTimeConverter {
    public static final String ID = "com.example.LocalDateConverter";

    @Override
    public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String value) {
        Object o = super.getAsObject(facesContext, uiComponent, value);

        if (o == null) {
            return null;
        }

        if (o instanceof Date) {
            Instant instant = Instant.ofEpochMilli(((Date) o).getTime());
            return LocalDateTime.ofInstant(instant, ZoneId.systemDefault()).toLocalDate();
        } else {
            throw new IllegalArgumentException(String.format("value=%s could not be converted to a LocalDate, result super.getAsObject=%s", value, o));
        }
    }

    @Override
    public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value) {
        if (value == null) {
            return super.getAsString(facesContext, uiComponent,value);
        }

        if (value instanceof LocalDate) {
            LocalDate lDate = (LocalDate) value;
            Instant instant = lDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant();
            Date date = Date.from(instant);
            return super.getAsString(facesContext, uiComponent, date);
        } else {
            throw new IllegalArgumentException(String.format("value=%s is not a instanceof LocalDate", value));
        }
    }
}

然后在LocalDateConverter-taglib.xml中创建一个文件META-INF:
<facelet-taglib version="2.2"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facelettaglibrary_2_2.xsd">

    <namespace>http://example.com/LocalDateConverter</namespace>
    <tag>
        <tag-name>convertLocalDate</tag-name>
        <converter>
            <converter-id>com.example.LocalDateConverter</converter-id>
        </converter>
    </tag>
</facelet-taglib>

最后,在web.xml中注册taglib:
<context-param>
    <param-name>javax.faces.FACELETS_LIBRARIES</param-name>
    <param-value>/META-INF/LocalDateConverter-taglib.xml</param-value>
</context-param>

用法
要在您的JSF-Page中使用新标签,请添加新的Taglib xmlns:ldc="http://example.com/LocalDateConverter"并使用该标签:
<ldc:convertLocalDate type="both" dateStyle="full"/>

09-25 20:45