简体   繁体   中英

JSF 2: f:convertDateTime inside a rich:dataTable

I have the next Java model bean (which contains not only a date in milliseconds but also a time zone)

public class Device {

    private Calendar lentDate;

    // getters and setters
}

and the next xhtml page fragment

<rich:dataTable value="#{tagBean.devices}" var="device">
    <rich:column>
        <h:outputText value="#{device.lentDate.time}">
            <f:convertDateTime pattern="dd.MM.yyyy" timeZone="#{device.lentDate.timeZone}"/>
        </h:outputText>
    </rich:column>
</rich:dataTable>

But the timeZone attribute does not get the device.lentDate.timeZone value. Looks like it is because when the f:convertDateTime tag is rendered, the device variable is not available yet.

Is it possible to force JSF to render the f:convertDateTime tag after the device variable is available? Or the only way to make the timeZone set properly for each device in this case is to create a custom date/time converter?

Thank you.

The problem is that the attributes of the converter are evaluated when the view is build. This is partially because a converter is not a component itself, but in Facelets implemented by a TagHandler that sets a converter instance in the parent component. See JSTL in JSF2 Facelets... makes sense? for a very thorough background explanation.

You can solve this by using the <o:converter> from OmniFaces that was specifically build for this use case. Contrary to the regular converters, this one does evaluate its attributes when its parent component is being rendered.

See: http://showcase.omnifaces.org/taghandlers/converter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM