我正在使用REST view服务,并在我的视图中直接使用JAXB对象。一个人的约会方式是这样的XMLGregorianCalendar:
@XmlAttribute(name = "record")
@XmlSchemaType(name = "dateTime")
protected XMLGregorianCalendar record;同时尝试使用标准转换器
<h:outputText value="#{bean.value.record}" >
<f:convertDateTime pattern="dd.MM.yy" />
</h:outputText> 我在我的JSF2环境中得到了错误消息(翻译成英文)(JBoss7.1.1-Final)
javax.faces.convert.ConverterException: fSelection:dtSelection:0:j_idt42:
Converting of '2012-07-25T20:15:00' into string not possible.默认转换器似乎不支持XMLGregorianCalendar类型。我想知道这个日期类型的JSF转换器是否可用,因为这个要求似乎并没有那么不寻常.
编辑 Ravi提供了一个自定义转换器的功能示例,但这似乎不够灵活:
发布于 2012-07-29 16:37:12
该值应为java.util.Date类型。
因此,从XMLGregorianCalendar中获取Date对象如下:
record.toGregorianCalendar().getTime();更新
你可以这样使用:
<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
发布于 2012-07-29 16:32:22
您可以注册一个自定义XML,以便按照以下代码将XMLGregorianCalendar转换为日历或日期:How do I customise date/time bindings using JAXWS and APT?
https://stackoverflow.com/questions/11709936
复制相似问题