Example:
<h:form>
<h:selectOneMenu value="#{bean.timezone}>
<f:selectItems value="#{bean.availableTimezones} ... >
<f:ajax render="currenttime" />
</h:selectOneMenu>
</h:form>
<h:form id="currenttime">
<h:outputText value="#{bean.currentTime}" >
<f:convertDateTime dateStyle="short" type="both" timeZone="#{bean.timezone}" />
</h:outputText>
</h:form>
<!-- bean.currentTime is of type 'Date' -->
In the example, changing the timezone should cause the text in currenttime to show in the proper timezone. But it doesn’t.
I figured this happens because converters are calculated in “Apply Request” phase and the value of the selected timezone is updated in “Update Model” phase.
Am I right?
Should I not use converters for this?
Thanks!
Your concrete problem is caused because
<f:convertDateTime>is initialized during view build time, not during view render time (exactly like JSTL and so on). Indeed, this runs far before update model values phase and hence the converter will not get set with the user-submitted timezone during view render time.This problem has basically the same grounds as answered in the following answers:
One of the ways is managing and binding the converter instance as a bean property.
With
An alternative is using OmniFaces
<o:converterwhich supports rendertime evaluation of converter’s properties: