【问题标题】:Composite Component with f:convertNumber won't work具有 f:convertNumber 的复合组件将不起作用
【发布时间】:2016-02-03 15:04:01
【问题描述】:

我制作了一个使用f:convertNumber 的JSF 复合组件。但是,它不能转换价值。这是如何引起的,我该如何解决?

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>

...

结果

10000000

【问题讨论】:

  • “无法转换”含糊不清。是否愿意更明确地描述问题以及实际发生了什么?
  • @Kukeltje 谢谢,我希望我的 Composit Componet(currency.xhtml) 将 h:outputText 的值转换为“10,000,000”作为模式属性 (#,###)。但是,它显示“10000000”。因此,我认为转换器无法转换值。

标签: jsf converter composite-component


【解决方案1】:

这确实行不通。

复合组件被解释为 UI 组件。然而,&lt;f:convertNumber&gt; 是一个标记处理程序,而不是一个 UI 组件。基本上,它将应用于复合材料本身(并呈现为无用),而不是按您的意图应用于目标组件。

你至少有两个选择:

  1. 也将&lt;h:outputText&gt; 移动到合成中,

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

    所以你最终可以像下面这样使用它。

    <mycomp:currency value="10000000" />
    
  2. 在构造函数中设置默认值的子类NumberConverter 并改用它。

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

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

    当您按照此处Creating custom tag for Converter with attributes 所述在标记文件中注册此转换器时,

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

    那么您最终可以按预期使用它。

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

另见:

【讨论】:

  • 它的工作。我仍然需要更多地学习 JSF。非常感谢。
  • 不客气。随意问以前没有问过的问题:)
猜你喜欢
  • 2012-04-08
  • 1970-01-01
  • 1970-01-01
  • 2013-05-03
  • 1970-01-01
  • 2017-02-24
  • 1970-01-01
  • 2011-04-30
  • 2014-03-11
相关资源
最近更新 更多