首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JSF: dateTimeConverter for XMLGregorianCalendar

JSF: dateTimeConverter for XMLGregorianCalendar
EN

Stack Overflow用户
提问于 2012-07-29 14:19:59
回答 2查看 6.7K关注 0票数 2

我正在使用REST view服务,并在我的视图中直接使用JAXB对象。一个人的约会方式是这样的XMLGregorianCalendar

代码语言:javascript
复制
@XmlAttribute(name = "record")
@XmlSchemaType(name = "dateTime")
protected XMLGregorianCalendar record;

同时尝试使用标准转换器

代码语言:javascript
复制
<h:outputText value="#{bean.value.record}" >
  <f:convertDateTime pattern="dd.MM.yy" />    
</h:outputText>  

我在我的JSF2环境中得到了错误消息(翻译成英文)(JBoss7.1.1-Final)

代码语言:javascript
复制
javax.faces.convert.ConverterException: fSelection:dtSelection:0:j_idt42:
Converting of '2012-07-25T20:15:00' into string not possible.

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

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

  • 这个模式是硬编码的。
  • 不支持本地用户
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-07-29 16:37:12

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

因此,从XMLGregorianCalendar中获取Date对象如下:

代码语言:javascript
复制
record.toGregorianCalendar().getTime();

更新

你可以这样使用:

代码语言:javascript
复制
<h:outputText value="#{bean.value.record.toGregorianCalendar().time}" >
    <f:convertDateTime pattern="dd.MM.yy" />    
</h:outputText>

这实际上应该是可行的,但既然你说你得到了一个IllegalAccessException,我不确定确切的原因。

或者,您也可以编写自己的转换器,如果愿意,转换器将如下所示:

如果要使用与dateTimeConverter相同的属性,则需要将它们作为属性传递给组件,并按如下方式扩展DateTimeConverter:

代码语言:javascript
复制
@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);
}

}

然后在你的页面上这样使用:

代码语言:javascript
复制
<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

票数 6
EN

Stack Overflow用户

发布于 2012-07-29 16:32:22

您可以注册一个自定义XML,以便按照以下代码将XMLGregorianCalendar转换为日历或日期:How do I customise date/time bindings using JAXWS and APT?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11709936

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档