2

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!

Ben
  • 10,020
  • 21
  • 94
  • 157

1 Answers1

6

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.

private DateTimeConverter converter;

@PostConstruct
public void init() {
    converter = new DateTimeConverter();
    converter.setDateStyle("short");
    converter.setType("both");
}

public DateTimeConverter getDateTimeConverter() {
    converter.setTimeZone(timezone);
    return converter;
}

With

<h:outputText value="#{bean.currentTime}" >
    <f:converter binding="#{bean.dateTimeConverter}" />
</h:outputText>

An alternative is using OmniFaces <o:converter which supports rendertime evaluation of converter's properties:

<h:outputText value="#{bean.currentTime}" >
    <o:converter converterId="javax.faces.DateTime" dateStyle="short" type="both" timeZone="#{bean.timezone}" />
</h:outputText>
Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555