简体   繁体   English

具有f:convertNumber的复合组件将无法正常工作

[英]Composite Component with f:convertNumber won't work

I made a JSF composite component which uses f:convertNumber . 我制作了一个使用f:convertNumber的JSF复合组件。 However, it cannot convert value. 但是,它不能转换值。 How is this caused and how can I solve it? 这是怎么引起的,我该如何解决?

currency.xhtml 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 的index.xhtml

...

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

...

result 结果

10000000

This will indeed not work. 这确实是行不通的。

Composite components are interpreted as UI components. 复合组件被解释为UI组件。 The <f:convertNumber> , however, is a taghandler, not an UI component. 但是, <f:convertNumber>是标记处理程序,而不是UI组件。 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, 也将<h:outputText>移到组合中,

     <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. 在构造函数中设置了默认值的NumberConverter子类,请改用它。

     @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 , 如此处所述,当您在标记文件中注册此转换器时,使用属性为Converter创建自定义标记

     <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: 也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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