0

I want to create a custom JSF component that shows a numeric value in a specified format, complete with tooltip showing the same value.

When I actually try and use my component, however, I get an exception caused by a failed conversion of the value attribute:

javax.faces.convert.ConverterException: form1:myAccordion:myComponentText: Could not convert '345' to a string.

My page is as follows:

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

Where

bean.getValue()
is of BigDecimal type and the component is defined as such:
<!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>

Lastly, my taglib file contains the following declaration:

<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>

If I force the returned value into text by using

value="#{text[value]}"
I don't cause any exceptions but I get a blank string instead.

I know it's feasible since, if I just up and place an outputText with convertNumber and tooltip in my page without resorting to custom components, it works perfectly.

How can I fix this issue?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
denf86
  • 1
  • 3
  • How did you want to convert `-` to a number by the way? Nonetheless, the problem is not visible in the information provided so far as it's not in a [MCVE format](http://stackoverflow.com/tags/jsf/info) at all. So I'll do an educated guess: is this tagfile included in an iterating JSF component like ui:repeat or h:dataTable? – BalusC Oct 22 '15 at 13:19
  • Yes, it's placed inside a p:dataTable. As for the hyphen, I wanted to use it to mark an empty value. – denf86 Oct 22 '15 at 13:21
  • There's the cause. Now, checkout the duplicate. – BalusC Oct 22 '15 at 13:22

0 Answers0