JSF自定义组件属性的转换错误

时间:2015-10-22 13:10:18

标签: jsf primefaces facelets

我想创建一个自定义的JSF组件,它以指定的格式显示数值,并且工具提示显示相同的值。

但是,当我实际尝试使用我的组件时,我会收到由value属性转换失败引起的异常:

  

javax.faces.convert.ConverterException:form1:myAccordion:myComponentText:无法转换' 345'到一个字符串。

我的页面如下:

<my:outputNumber id="myComponent" 
    value="#{bean.value}" tooltip="true" />

其中

bean.getValue()
属于BigDecimal类型,组件定义如下:

<!DOCTYPE composition 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:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:c="http://java.sun.com/jstl/core"
    xmlns:p="http://primefaces.org/ui"
    xmlns:pe="http://primefaces.org/ui/extensions"
    xmlns:composite="http://java.sun.com/jsf/composite">
<ui:composition>
    <c:if test="{empty type}">
        <c:set var="type" value="number"/>
    </c:if>
    <c:if test="{empty decimals}">
        <c:set var="decimals" value="2"/>
    </c:if>
    <c:if test="{empty tooltip}">
        <c:set var="tooltip" value="false"/>
    </c:if>
    <c:when test="#{tooltip == 'byValue'}">
        <c:set var="renderTooltip" value="#{empty value}" />
    </c:when>
    <c:otherwise>
        <c:set var="renderTooltop" value="#{tooltip}" />
    </c:otherwise>
    <p:outputPanel id="#{id}" styleClass="my-output-number #{styleClass}">
        <h:outputText id="#{id}Text" value="#{empty value? '-' : value}" styleClass="my-output-number-value">
            <f:convertNumber type="#{type}" locale="IT_it"
                minFractionDigits="#{decimals}" maxFractionDigits="#{decimals}" />
        </h:outputText>
        <c:if test="#{tooltip}">
            <pe:tooltip for="#{id}Tooltip" value="#{empty value? '-' : value}" 
                rendered="#{renderTooltip}" styleClass="my-output-number-tooltip">
                <f:convertNumber type="#{type}" locale="IT_it" 
                    minFractionDigits="#{decimals}" maxFractionDigits="#{decimals}" />
            </pe:tooltip>
        </c:if>
    </p:outputPanel>
</ui:composition>

最后,我的taglib文件包含以下声明:

<tag>
    <tag-name>outputNumber</tag-name>
    <source>../layouts/component/outputNumber.xhtml</source>
    <attribute>
        <name>id</name>
        <required>true</required>
    </attribute>
    <attribute>
        <name>value</name>
        <required>true</required>
    </attribute>
    <attribute>
        <name>type</name>
    </attribute>
    <attribute>
        <name>decimals</name>
        <type>int</type>
    </attribute>
    <attribute>
        <name>tooltip</name>
    </attribute>
    <attribute>
        <name>styleClass</name>
    </attribute>
</tag>

如果我使用

value="#{text[value]}"
强制将返回的值转换为文本,我不会导致任何异常,但我会得到一个空字符串。

我知道这是可行的,因为如果我只是在我的页面中放置一个带有convertNumber和tooltip的outputText而不需要使用自定义组件,它就能完美运行。

如何解决此问题?

0 个答案:

没有答案
相关问题