f:convertDateTime支持Java8 LocalDate / LocalDateTime? [英] f:convertDateTime support for Java8 LocalDate / LocalDateTime?
问题描述
JSF核心标记 f:convertDateTime 可以格式 java.util.Date 对象。 Date类有许多不推荐使用的方法,Java 8提供了新类来显示本地日期和时间: LocalDateTime 和 LocalDate 。
f:convertDateTime 无法格式化 LocalDateTime 也不 LOCALDATE 。
有没有人知道,如果有一个等价的JSF核心标签convertDateTime可以处理LocalDateTime对象?计划是为将来的版本提供支持,还是提供备用标签?
编写自己的转换器并扩展 javax.faces.convert.DateTimeConverter
- 这样你可以重用< f:convertDateTime>
支持的所有属性。它也会照顾本地化。不幸的是,编写带属性的转换器有点复杂。
- 编写自己的扩展
的转换器javax.faces .convert.DateTimeConverter
- 让超级调用完成所有工作(包括locale-stuff)并将结果从/转换为LocalDate。
'pre>
@FacesConverter(值= LocalDateConverter.ID)
公共类LocalDateConverter延伸DateTimeConverter {
公共静态最终字符串ID = &#X22; com.example.LocalDateConverter&#X22 ;;
&#x9;
&#x9; @Override
&#x9; public Object getAsObject(FacesContext facesContext,
&#x9;&#x9;&#x9; UIComponent uiComponent,String value){
&#x9; &#x9; LocalDate ldate = null;
&#x9; &#x9;日期日期= null;
&#x9; &#x9; Object o = super.getAsObject(facesContext,uiComponent,value);
&#x9; &#X9; if(o == null){
&#x9;&#x9;&#x9;返回null;
&#x9;&#x9;}
&#x9; &#x9; if(o instanceof Date){
&#x9; &#x9;&#x9; date =(Date)o;
&#x9; &#x9;&#x9;即时瞬间= Instant.ofEpochMilli(date.getTime());
&#x9; &#x9;&#x9; ldate = LocalDateTime.ofInstant(instant,ZoneId.systemDefault())。toLocalDate();
&#x9; &#x9;&#x9;返回ldate;
&#x9; &#x9;}
&#x9; &#x9;否则{
&#x9; &#x9;&#x9;抛出新的IllegalArgumentException(String.format(&#x22;值=%s无法转换为LocalDate,结果为super.getAsObject =%s&#x22;,value,o));
&#x9; &#x9;}
&#x9; }
&#x9;
&#x9; @Override
&#x9;公共字符串符getAsString(FacesContext中的FacesContext,
&#X9;&#X9;&#X9; UIComponent UIComponent,而对象的值){
&#X9;&#X9;如果(值== NULL){
&#x9;&#x9;&#x9;返回super.getAsString(facesContext,uiComponent,value);
&#x9;&#x9;}
&#x9; &#x9; if(value instanceof LocalDate){
&#x9; &#x9;&#x9; LocalDate lDate =(LocalDate)值;
&#x9; &#x9;&#x9; Instant instant = lDate.atStartOfDay()。atZone(ZoneId.systemDefault())。toInstant();
&#x9; &#x9;&#x9;日期日期= Date.from(即时);
&#x9; &#x9;&#x9;返回super.getAsString(facesContext,uiComponent,date);
&#x9; &#x9;}
&#x9; &#x9;否则{
&#x9; &#x9;&#x9;抛出新的IllegalArgumentException(String.format(&#x22; value =%s不是LocalDate的实例&#x22;,value));
&#x9; &#x9;}
&#x9; }
&#x9;
}
- 创建文件
LocalDateConverter-taglib.xml
inMETA-INF
:
&#x3C; facelet-taglib version =&#x22; 2.2&#x22;
xmlns =&#x22; http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi =&#x22; http://www.w3.org/2001/XMLSchema-instance"
的xsi:的schemaLocation =&#X22; HTTP://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facelettaglibrary_2_2.xsd" ;&#x3E;
&#x3C; namespace&#x3E; http://example.com/LocalDateConverter< / namespace&#x3E;
&#x3C; tag&#x3E;
&#x3C; tag-name&#x3E; convertLocalDate&#x3C; / tag-name&#x3E;
&#x3C; converter&#x3E;
&#x3C; converter-id&#x3E; com.example.LocalDateConverter&#x3C; / converter-id&#x3E;
&#x3C; / converter&#x3E;
&#x3C; / tag&#x3E;
&#x3C; / facelet-taglib&#x3E;
- 在
web中注册taglib。 xml
:
&#x3C; context-param& #x3E;
&#x3C; param-name&#x3E; facelets.LIBRARIES&#x3C; / param-name&#x3E;
&#x3C; param-value&#x3E; /META-INF/LocalDateConverter-taglib.xml< / param-value&#x3E;
&#x3C; / context-param&#x3E;
- 在JSF-Page中使用新标签添加新Taglib
xmlns:ldc =http://example.com/LocalDateConverter
然后你可以使用Tag < ldc:convertLocalDate type =bothdateStyle =full/>
。
The JSF Core Tag f:convertDateTime can format java.util.Date objects. The Date class has many deprecated methods and with Java 8 come new classes to present local dates and times: LocalDateTime and LocalDate.
f:convertDateTime can not format LocalDateTime nor LocalDate.
Does anybody know, if there is an equivalent to the JSF core tag convertDateTime that can deal with LocalDateTime objects? Is support planned for a future release, or are alternative tags available?
Write your own Converter and extend the javax.faces.convert.DateTimeConverter
- that way you can Reuse all the attributes that <f:convertDateTime>
supports. Also it will take care of Localization too. Unfortunately its a bit more complicated to write a Converter with Attributes.
- Write your own Converter that extends
javax.faces.convert.DateTimeConverter
- just let the super-calls do all the work (including locale-stuff) and convert the result from/to 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) { LocalDate ldate = null; Date date = null; Object o = super.getAsObject(facesContext, uiComponent, value); if(o == null) { return null; } if (o instanceof Date) { date = (Date) o; Instant instant = Instant.ofEpochMilli(date.getTime()); ldate = LocalDateTime.ofInstant(instant, ZoneId.systemDefault()).toLocalDate(); return ldate; } 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)); } } }
- Create a file
LocalDateConverter-taglib.xml
inMETA-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>
- Register that taglib in
web.xml
:
<context-param> <param-name>facelets.LIBRARIES</param-name> <param-value>/META-INF/LocalDateConverter-taglib.xml</param-value> </context-param>
- To use the new Tag in your JSF-Page add the new Taglib
xmlns:ldc="http://example.com/LocalDateConverter"
Then you can use the Tag <ldc:convertLocalDate type="both" dateStyle="full"/>
.
这篇关于f:convertDateTime支持Java8 LocalDate / LocalDateTime?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!