Search code examples
jsfconverterscomposite-component

Composite Component with f:convertNumber won't work


I made a JSF composite component which uses f:convertNumber. However, it cannot convert value. How is this caused and how can I solve it?

currency.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:composite="http://java.sun.com/jsf/composite"
    xmlns:f="http://java.sun.com/jsf/core">

    <composite:interface>
    </composite:interface>

    <composite:implementation>
        <f:convertNumber pattern="#,###" currencyCode="\\"/> 
    </composite:implementation>
</html>

index.xhtml

...

<h:outputText value="10000000">
  <mycomp:currency />
</h:outputText>

...

result

10000000

Solution

  • This will indeed not work.

    Composite components are interpreted as UI components. The <f:convertNumber>, however, is a taghandler, not an UI component. Basically, it will get applied on the composite itself (and render as useless), not on the target component as you intented.

    You've at least two options:

    1. Move <h:outputText> into the composite too,

      <composite:interface>
          <composite:attribute name="value" />
      </composite:interface>
      <composite:implementation>
          <h:outputText value="#{cc.attrs.value}">
              <f:convertNumber pattern="#,###" currencyCode="\\" />
          </h:outputText>
      </composite:implementation>
      

      so you can ultimately use it as below.

      <mycomp:currency value="10000000" />
      
    2. Subclass NumberConverter with default values set in constructor and use it instead.

      @FacesConverter("defaultCurrencyConverter")
      public class DefaultCurrencyConverter extends NumberConverter {
      
          public DefaultCurrencyConverter() {
              setPattern("#,###");
              setCurrencyCode("\\");
          }
      
      }
      

      <h:outputText value="10000000" converter="defaultCurrencyConverter" />
      

      When you register this converter in a tagfile as described here Creating custom tag for Converter with attributes,

      <tag>
          <tag-name>currency</tag-name>
          <converter>
              <converter-id>defaultCurrencyConverter</converter-id>
          </converter>
      </tag>
      

      then you can ultimately use it as intented.

      <h:outputText value="10000000">
          <mycomp:currency />
      </h:outputText>
      

    See also: