2012-07-29 35 views
2

我正在使用REST Web服務並直接在我的視圖中使用JAXB對象。其中有一個日期作爲XMLGregorianCalendar這樣的:雖然想在我JSF2環境(JBoss的7.1使用標準轉換器JSF:用於XMLGregorianCalendar的dateTimeConverter

<h:outputText value="#{bean.value.record}" > 
    <f:convertDateTime pattern="dd.MM.yy" />  
</h:outputText> 

我得到錯誤信息(翻譯成英文)

@XmlAttribute(name = "record") 
@XmlSchemaType(name = "dateTime") 
protected XMLGregorianCalendar record; 

。 1 - 決賽)

javax.faces.convert.ConverterException: fSelection:dtSelection:0:j_idt42: 
Converting of '2012-07-25T20:15:00' into string not possible. 

看來,類型XMLGregorianCalendar不被默認支持的轉換器。我想知道這個日期類型的JSF轉換器是否可用,因爲這個要求似乎並不是那麼不尋常...

編輯 Ravi提供了一個自定義轉換器的功能示例,但這似乎是到不易伸縮:

  • 圖案硬編碼
  • 用戶本地

回答

5

該值應該是java.util.Date類型。

因此,從的XMLGregorianCalendar獲得Date對象是這樣的:

record.toGregorianCalendar().getTime(); 

UPDATE

你可以使用這樣的:

<h:outputText value="#{bean.value.record.toGregorianCalendar().time}" > 
    <f:convertDateTime pattern="dd.MM.yy" />  
</h:outputText> 

這實際上應該工作,但因爲你說你得到一個IllegalAccessException,我不確定是否有確切的原因。

另外,您也可以編寫自己的轉換器,如果你想,轉換器將是這樣的:

如果你想使用,你會用dateTimeConverter使用相同的屬性,那麼你需要將它們傳遞的屬性中組件和擴展DateTimeConverter這樣的:

@FacesConverter("com.examples.Date") 
public class XMLGregorianCalConverter extends DateTimeConverter { 
private static final TimeZone DEFAULT_TIME_ZONE = TimeZone.getTimeZone("GMT"); 
private String dateStyle = "default"; 
private Locale locale = null; 
private String pattern = null; 
private String timeStyle = "default"; 
private TimeZone timeZone = DEFAULT_TIME_ZONE; 
private String type = "date"; 

@Override 
public Object getAsObject(FacesContext context, UIComponent component, String newValue) { 
    // TODO Auto-generated method stub 
    return null; 
} 

@Override 
public String getAsString(FacesContext context, UIComponent component, Object value) { 
    Map<String, Object> attributes = component.getAttributes(); 
    if(attributes.containsKey("pattern")){ 
     pattern = (String) attributes.get("pattern"); 
    } 
    setPattern(pattern); 
    if(attributes.containsKey("locale")){ 
     locale = (Locale) attributes.get("locale"); 
    } 
    setLocale(locale); 
    if(attributes.containsKey("timeZone")){ 
     timeZone = (TimeZone) attributes.get("timeZone"); 
    } 
    setTimeZone(timeZone); 
    if(attributes.containsKey("dateStyle")){ 
     dateStyle = (String) attributes.get("dateStyle"); 
    } 
    setDateStyle(dateStyle); 
    if(attributes.containsKey("timeStyle")){ 
     timeStyle = (String) attributes.get("timeStyle"); 
    } 
    setTimeStyle(timeStyle); 
    if(attributes.containsKey("type")){ 
     type = (String) attributes.get("type"); 
    } 
    setType(type); 

    XMLGregorianCalendar xmlGregCal = (XMLGregorianCalendar) value; 
    Date date = xmlGregCal.toGregorianCalendar().getTime(); 
    return super.getAsString(context, component, date); 
} 

} 

,並在網頁上使用這樣的:

<h:outputText value="#{bean.value.record}" > 
    <f:converter converterId="com.examples.Date" /> 
    <f:attribute name="pattern" value="dd.MM.yy" /> 
</h:outputText> 

來自此問題的啓發/複製的代碼:JSF convertDateTime with timezone in datatable

+0

是的,在Java中很容易,但是如何在JSF中執行此操作? – Thor 2012-07-29 18:09:27

+0

Update1引發IllegalAccessException。正如預期的那樣,您的替代方法正在工作,但不靈活,因爲_pattern_在「XMLGregorianCalConverter」中進行了硬編碼。 – Thor 2012-07-29 19:16:46

+0

@Thor,我已經更新了關於您的問題的答案。 – Ravi 2012-07-30 02:55:58