【发布时间】:2023-12-28 04:06:01
【问题描述】:
基本的 Joda 时间转换器(代码对于这个线程的上下文绝对是多余的):
@Named
@ApplicationScoped
@FacesConverter(forClass = DateTime.class)
public class DateTimeConverter implements Converter {
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
if (value == null || value.isEmpty()) {
return null;
}
try {
return DateTimeFormat.forPattern("dd-MMM-yyyy hh:mm:ss aa Z").parseDateTime(value).withZone(DateTimeZone.UTC);
} catch (IllegalArgumentException | UnsupportedOperationException e) {
throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, null, "Message"), e);
}
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
if (value == null) {
return "";
}
if (!(value instanceof DateTime)) {
throw new ConverterException("Error");
}
try {
return DateTimeFormat.forPattern("dd-MMM-yyyy hh:mm:ss aa Z").print(((DateTime) value).withZone(DateTimeZone.forID("zoneId")));
} catch (IllegalArgumentException e) {
throw new ConverterException("Error", e); // Not required.
}
}
}
为什么它不能与 <p:calendar> 一起使用,除非/直到使用 converter 属性明确指定它?
<p:calendar converter="#{dateTimeConverter}" value="{bean.dateTimeValue}" .../>
与其他组件一样,预计它可以在不提及converter 属性的情况下工作,因为转换器被装饰了
@FacesConverter(forClass = DateTime.class)
<p:calendar>不支持这个功能吗?
使用 PrimeFaces 5.2 和 JSF 2.2.12。
【问题讨论】:
标签: jsf primefaces calendar converter jsf-2.2