-
Available since OmniFaces 1.0
The <o:converter>
is a taghandler that extends the standard <f:converter>
tag family with support for deferred value expressions in all attributes. In other words, the converter attributes are not evaluated anymore on a per view build time basis, but just on every access like as with UI components and bean properties. This has among others the advantage that they can be evaluated on a per-iteration basis inside an iterating component, and that they can be set on a custom converter without needing to explicitly register it in a tagfile.
Usage
When you specify for example the standard <f:convertDateTime>
by converterId="jakarta.faces.DateTime"
, then you'll be able to use all its attributes such as pattern
and locale
as per its documentation, but then with the possibility to supply deferred value expressions.
<o:converter converterId="jakarta.faces.DateTime" pattern="#{item.pattern}" locale="#{item.locale}" />
The converter ID of all standard Faces converters can be found in their javadocs. First go to the javadoc of the class of interest, then go to CONVERTER_ID
in its field summary and finally click the Constant Field Values link to see the value.
JSF 2.3 compatibility
The <o:converter>
is currently not compatible with converters which are managed via the managed=true
attribute set on the FacesConverter
annotation, at least not when using Mojarra. Internally, the converters are wrapped in another instance which doesn't have the needed setter methods specified. In order to get them to work with <o:converter>
, the managed=true
attribute needs to be removed, so that OmniFaces ConverterManager
will automatically manage them.
Demo
In the below demo, it was the intent to use <f:convertDateTime>
as follows:
<f:convertDateTime type="localDateTime" dateStyle="full" timeStyle="full" locale="#{locale}" />
But this wouldn't even run, it will throw the following exception because the #{locale}
isn't
available during view build time (for a background explanation, check
this Stack Overflow answer).
jakarta.faces.view.facelets.TagAttributeException: /showcase/taghandlers/converter.xhtml @53,101 locale="" Attribute did not evaluate to a String or Locale: null at com.sun.jsf-impl@4.0.0.SP01//com.sun.faces.facelets.tag.faces.ComponentSupport.getLocale(ComponentSupport.java:423) at com.sun.jsf-impl@4.0.0.SP01//com.sun.faces.facelets.tag.faces.core.ConvertDateTimeHandler.setAttributes(ConvertDateTimeHandler.java:89) at com.sun.jsf-impl@4.0.0.SP01//com.sun.faces.facelets.tag.faces.ConverterTagHandlerDelegateImpl.applyAttachedObject(ConverterTagHandlerDelegateImpl.java:117)
It works as intuitively expected with the <o:converter>
as it allows a render time evaluation of attributes.
For another related use case, checkout the
<o:validator>
showcase page.
Language | Full date time pattern |
---|---|
English | Thursday, November 21, 2024 at 1:18:39 AM Central Standard Time |
Spanish | jueves, 21 de noviembre de 2024, 1:18:39 (hora estándar central) |
French | jeudi 21 novembre 2024 à 01:18:39 heure normale du centre nord-américain |
German | Donnerstag, 21. November 2024 um 01:18:39 Nordamerikanische Inland-Normalzeit |
Dutch | donderdag 21 november 2024 om 01:18:39 Central-standaardtijd |
Arabic | الخميس، 21 نوفمبر 2024 1:18:39 ص التوقيت الرسمي المركزي لأمريكا الشمالية |
Hebrew | יום חמישי, 21 בנובמבר 2024 בשעה 1:18:39 שעון מרכז ארה״ב (חורף) |
Chinese | 2024年11月21日星期四 北美中部标准时间 上午1:18:39 |
<h:dataTable value="#{converterBean.locales}" var="locale">
<h:column>
<f:facet name="header">Language</f:facet>
#{locale.displayLanguage}
</h:column>
<h:column>
<f:facet name="header">Full date time pattern</f:facet>
<h:outputText value="#{now.zonedDateTime}">
<o:converter converterId="jakarta.faces.DateTime" type="localDateTime" dateStyle="full" timeStyle="full" locale="#{locale}" />
</h:outputText>
</h:column>
</h:dataTable>
package org.omnifaces.showcase.taghandlers;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import jakarta.annotation.PostConstruct;
import jakarta.enterprise.context.RequestScoped;
import jakarta.inject.Named;
@Named
@RequestScoped
public class ConverterBean {
private List<Locale> locales;
@PostConstruct
public void init() {
locales = new ArrayList<>();
for (String language : new String[] { "en", "es", "fr", "de", "nl", "ar", "he", "zh" }) {
locales.add(new Locale(language));
}
}
public List<Locale> getLocales() {
return locales;
}
}