2016-11-05 58 views
0

我在j2ee的新手,並有一個問題,我不能解決。我的應用程序使用Spring,Hibernate4和JSF 2 * 我的模型實體(其名稱任務)有下一個字段:通過JSF轉換日期和LocalDateTime

@Convert(converter = LocalDateTimeConverter.class) 
@Column(name = "date_time_complete") 
private LocalDateTime completeDate; 

在這種情況下,轉換到LocalDateTime日期,相反我做了轉換器

@Converter 
public class LocalDateTimeConverter implements AttributeConverter<LocalDateTime, Date> { 

    public Date convertToDatabaseColumn(LocalDateTime attribute) { 
     return TimeUtil.LDTtoDate(attribute); 
    } 

    public LocalDateTime convertToEntityAttribute(Date dbData) { 
     return LocalDateTime.ofInstant(dbData.toInstant(), ZoneId.systemDefault()); 
    } 
} 

在我.xhtml我有一個代碼

<td><h:inputText id="date" value="#{task.completeDate}"> 
        <f:convertDateTime pattern="dd.MM.yyyy"/> 
       </h:inputText> 

當我試圖挽救我的目標,我得到ELException與消息「無法轉換java.util.Date到java.time.Loc alDateTime「。 調試應用程序我在convertToEntityAttribute中創建斷點,但它不起作用。 通常,我想知道jsf如何使用LocalDateTime。 在這個應用程序中,我不需要輸入completeDate字段。創建Task對象時,我需要保持這個空/空。我該怎麼做?

回答

0

JSF有一些默認轉換器。它們負責將視圖數據(例如在輸入文本中)轉換爲bean atrribute。就像將一個inputText轉換爲Double屬性一樣。希望Jsf提供了一種創建我們自己的轉換器的方法,你應該看看這個tutorialthis one too。你可以創建一個轉換器到你的LocalDateTime屬性!如果你想直接爲代碼,這傢伙有同樣的問題,你的解決與jsf轉換器! See here his solution