jsf - @FacesConverter(forClass = Clazz.class) and p:calendar -
basic joda time converter (the code absolutely superfluous context of thread) :
@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. } } }
why not work <p:calendar>
unless/until explicitly specified using converter
attribute?
<p:calendar converter="#{datetimeconverter}" value="{bean.datetimevalue}" .../>
like other components, expected work without mentioning converter
attribute because converter decorated with
@facesconverter(forclass = datetime.class)
is feature not supported <p:calendar>
?
using primefaces 5.2 , jsf 2.2.12.
based on 5.2's calendarrenderer#encodeend()
, uses calendarutils#getvalueasstring()
obtain value output. indeed doesn't consult via application#createconverter(class)
if there's converter class.
51 //first ask converter 52 if(calendar.getconverter() != null) { 53 return calendar.getconverter().getasstring(context, calendar, value); 54 } 55 //use built-in converter 56 else { 57 simpledateformat dateformat = new simpledateformat(calendar.calculatepattern(), calendar.calculatelocale(context)); 58 dateformat.settimezone(calendar.calculatetimezone()); 59 60 return dateformat.format(value); 61 }
that confirms behavior observed. you'd best create issue report primefaces guys requests adding instanceof date
check in place, , in opposite case obtain converter class application below:
converter converter = context.getapplication().createconverter(value.getclass()); if (converter != null) { return converter.getasstring(context, calendar, value); } else { throw new illegalargumentexception(value.getclass()); }
this indeed make sense given increasing use of java8's java.time
api. there way few other places in calendarrenderer
, calendarutils
could/should implemented (and performing instanceof
check, not delegating converter class, if any).
Comments
Post a Comment