Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JSF:XMLGregorianCalendar的日期时间转换器_Java_Jsf_Jaxb_Converter - Fatal编程技术网

Java JSF:XMLGregorianCalendar的日期时间转换器

Java JSF:XMLGregorianCalendar的日期时间转换器,java,jsf,jaxb,converter,Java,Jsf,Jaxb,Converter,我正在使用RESTWebService并直接使用视图中的JAXB对象。一个人的日期是xmlgoriancalendar,如下所示: @XmlAttribute(name = "record") @XmlSchemaType(name = "dateTime") protected XMLGregorianCalendar record; record.toGregorianCalendar().getTime(); <h:outputText value="#{bean.value.r

我正在使用RESTWebService并直接使用视图中的JAXB对象。一个人的日期是
xmlgoriancalendar
,如下所示:

@XmlAttribute(name = "record")
@XmlSchemaType(name = "dateTime")
protected XMLGregorianCalendar record;
record.toGregorianCalendar().getTime();
<h:outputText value="#{bean.value.record.toGregorianCalendar().time}" >
    <f:convertDateTime pattern="dd.MM.yy" />    
</h:outputText>
@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>
在尝试使用标准转换器时

<h:outputText value="#{bean.value.record}" >
  <f:convertDateTime pattern="dd.MM.yy" />    
</h:outputText>  
似乎默认转换器不支持类型
XMLGregorianCalendar
。我想知道这个日期类型的JSF转换器是否可用,因为这个需求似乎并没有那么不寻常

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

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

您可以注册一个自定义XML适配器,将XMLGregorianCalendar转换为日历或日期,具体如下:

值的类型应为java.util.Date

因此,从XMLGregorianCalendar获取日期对象,如下所示:

@XmlAttribute(name = "record")
@XmlSchemaType(name = "dateTime")
protected XMLGregorianCalendar record;
record.toGregorianCalendar().getTime();
<h:outputText value="#{bean.value.record.toGregorianCalendar().time}" >
    <f:convertDateTime pattern="dd.MM.yy" />    
</h:outputText>
@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>
更新

您可以这样使用:

@XmlAttribute(name = "record")
@XmlSchemaType(name = "dateTime")
protected XMLGregorianCalendar record;
record.toGregorianCalendar().getTime();
<h:outputText value="#{bean.value.record.toGregorianCalendar().time}" >
    <f:convertDateTime pattern="dd.MM.yy" />    
</h:outputText>
@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>

这实际上应该是可行的,但因为你说你得到了一个非法的访问例外,我不确定确切的原因

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

@XmlAttribute(name = "record")
@XmlSchemaType(name = "dateTime")
protected XMLGregorianCalendar record;
record.toGregorianCalendar().getTime();
<h:outputText value="#{bean.value.record.toGregorianCalendar().time}" >
    <f:convertDateTime pattern="dd.MM.yy" />    
</h:outputText>
@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>
如果要使用与dateTimeConverter相同的属性,则需要将它们作为属性传递给组件,并像下面这样扩展dateTimeConverter:

@XmlAttribute(name = "record")
@XmlSchemaType(name = "dateTime")
protected XMLGregorianCalendar record;
record.toGregorianCalendar().getTime();
<h:outputText value="#{bean.value.record.toGregorianCalendar().time}" >
    <f:convertDateTime pattern="dd.MM.yy" />    
</h:outputText>
@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>
@FacesConverter(“com.examples.Date”)
公共类XmlGregorianConverter扩展了DateTimeConverter{
私有静态最终时区默认值\u TIME\u ZONE=TimeZone.getTimeZone(“GMT”);
私有字符串dateStyle=“default”;
私有区域设置=null;
私有字符串模式=null;
私有字符串timeStyle=“default”;
专用时区时区=默认时区;
私有字符串type=“日期”;
@凌驾
公共对象getAsObject(FacesContext上下文、UIComponent组件、字符串newValue){
//TODO自动生成的方法存根
返回null;
}
@凌驾
公共字符串getAsString(FacesContext上下文、UIComponent、对象值){
映射属性=component.getAttributes();
if(attributes.containsKey(“模式”)){
pattern=(String)attributes.get(“pattern”);
}
设置模式(模式);
if(attributes.containsKey(“locale”)){
locale=(locale)attributes.get(“locale”);
}
setLocale(locale);
if(attributes.containsKey(“时区”)){
timeZone=(timeZone)attributes.get(“timeZone”);
}
设置时区(时区);
if(attributes.containsKey(“日期样式”)){
dateStyle=(String)attributes.get(“dateStyle”);
}
setDateStyle(dateStyle);
if(attributes.containsKey(“时间样式”)){
timeStyle=(String)attributes.get(“timeStyle”);
}
setTimeStyle(timeStyle);
if(attributes.containsKey(“类型”)){
type=(String)attributes.get(“type”);
}
setType(类型);
XMLGregorianCalendar xmlGregCal=(XMLGregorianCalendar)值;
Date Date=xmlGregCal.toGregorianCalendar().getTime();
返回super.getAsString(上下文、组件、日期);
}
}
并在您的页面上使用如下内容:

@XmlAttribute(name = "record")
@XmlSchemaType(name = "dateTime")
protected XMLGregorianCalendar record;
record.toGregorianCalendar().getTime();
<h:outputText value="#{bean.value.record.toGregorianCalendar().time}" >
    <f:convertDateTime pattern="dd.MM.yy" />    
</h:outputText>
@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>


代码灵感来源于/复制自以下问题:

我正在使用一个包含JAXB类的提供库,因此XML适配器不是一个选项。是的,在Java中很容易,但在JSF中如何做到这一点?Update1抛出了一个IllegaccessException。正如预期的那样,您的替代方案是有效的,但不灵活,因为模式是在
XMLGregorianConverter
中硬编码的。