Composite Component with f:convertNumber won't work

时光怂恿深爱的人放手 提交于 2020-01-25 01:51:57

问题


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

回答1:


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:

  • How to avoid repetitions / use constants in Facelets page?
  • When to use <ui:include>, tag files, composite components and/or custom components?


来源:https://stackoverflow.com/questions/35180537/composite-component-with-fconvertnumber-wont-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!