JSF:XMLGregorianCalendar的dateTimeConverter

Tho*_*hor 2 java jsf converter jaxb

我正在使用REST Web服务并在我的视图中直接使用JAXB对象.一个XMLGregorianCalendar像这样的日期:

@XmlAttribute(name = "record")
@XmlSchemaType(name = "dateTime")
protected XMLGregorianCalendar record;
Run Code Online (Sandbox Code Playgroud)

在尝试使用标准转换器时

<h:outputText value="#{bean.value.record}" >
  <f:convertDateTime pattern="dd.MM.yy" />    
</h:outputText>  
Run Code Online (Sandbox Code Playgroud)

我在JSF2环境中收到错误消息(翻译成英文)(JBoss-7.1.1-Final)

javax.faces.convert.ConverterException: fSelection:dtSelection:0:j_idt42:
Converting of '2012-07-25T20:15:00' into string not possible.
Run Code Online (Sandbox Code Playgroud)

看来,XMLGregorianCalendar默认转换器不支持该类型.我想知道这个日期类型的JSF转换器是否可用,因为这个要求似乎并不那么不寻常......

编辑 Ravi提供了一个自定义转换器的功能示例,但这似乎是不灵活的:

  • 模式是硬编码的
  • 不支持本地用户

Rav*_*avi 6

该值应为java.util.Date类型.

所以从XMLGregorianCalendar获取Date对象,如下所示:

record.toGregorianCalendar().getTime();
Run Code Online (Sandbox Code Playgroud)

更新:

你可以像这样使用:

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

这实际上应该可以工作但是因为你说你得到了一个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);
}

}
Run Code Online (Sandbox Code Playgroud)

并在您的页面上使用如下:

<h:outputText value="#{bean.value.record}" >
    <f:converter converterId="com.examples.Date" />
    <f:attribute name="pattern" value="dd.MM.yy" />
</h:outputText>
Run Code Online (Sandbox Code Playgroud)

代码启发/复制从这个问题:JSF convertDateTime与数据表中的时区