I have written a custom taglib to extend the converters already utilized in
JSF. The page is rendered, but with out the desired conversion. Debugging results in the invocation of the class, but the getAsString() or getAsObject methods do not get called. Can someone tell me what I am missing? The objective is to read a long from a database, convert it to hex and display.
No exceptions are thrown, the debugger just hits the class declaration and returns.
- Lou Caudell
################################## CONFIGURATION
OS: XPP SP2
IDE: Netbeans 5.5.1
JDK: 1.5.0_10
WAS: SJSAS 9.1
##################################################### faces-config.xml
<converter>
<converter-id>convert2Color</converter-id>
<converter-class>customconverter1.Convert2Color</converter-class>
</converter>
#####################################################
JSP <ui:tableColumn binding="#{Page1.tableColumn7}" headerText="forecolor" id="tableColumn7" sort="forecolor">
<ui:staticText binding="#{Page1.staticText7}" id="staticText7" text="#{currentRow.value['forecolor']}">
<ui:message binding="#{Page1.message1}" id="message1" showDetail="false" showSummary="true"/>
<f:converter converterId="convert2Color"/>
</ui:staticText>
</ui:tableColumn>
<ui:tableColumn binding="#{Page1.tableColumn8}" headerText="backcolor" id="tableColumn8" sort="backcolor">
<ui:staticText binding="#{Page1.staticText8}" id="staticText8" ext="#{currentRow.value['backcolor']}">
<f:converter converterId="convert2Color"/>
</ui:staticText>
<ui:message binding="#{Page1.message2}" id="message2" showDetail="false" showSummary="true"/>
</ui:tableColumn>
##################################################### class
package customconverter1;
import java.io.Serializable;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import org.apache.commons.lang.StringUtils;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
// ------------------------------------------------------------- Convert2Color()
public class Convert2Color implements Converter
{
// ============================================================= getAsObject()
public Object getAsObject(FacesContext context, UIComponent component,
String value) throws ConverterException
{
if (StringUtils.isEmpty(value)) { return null;}
HTMLColor hColor = new HTMLColor();
hColor.setOriginal(value);
hColor.setHexColor(value);
return hColor;
}
// ============================================================= getAsString()
public String getAsString(FacesContext context, UIComponent component, Object value) throws ConverterException
{
return value.toString();
}
} // class
// class ------------------------------------------------------------- HTMLColor
// -----------------------------------------------------------------------------
class HTMLColor implements Serializable
{
private String asColor = new String();
private String original = new String();
// ================================================================ toString()
public String toString()
{
return asColor;
}
// ============================================================= setOriginal()
public void setOriginal( String ocolor )
{
original = ocolor;
} // method
// ============================================================= setHexColor()
public void setHexColor( String longStr )
{
String result = new String();
int color = Integer.parseInt(longStr);
result = Integer.toHexString( color );
asColor = padColor( result );
} // method
// ================================================================ padColor()
// pad value with zeros is cases where string length less than 6
private String padColor( String result )
{
String padded = new String();
switch (result.length())
{
case 1:
padded = "00000"+result;
break;
case 2:
padded = "0000"+result;
break;
case 3:
padded = "000"+result;
break;
case 4:
padded = "00"+result;
break;
case 5:
padded = "0"+result;
break;
case 6:
padded = result;
break;
default:
return "not web color";
}
return padded;
} // method
} // class