Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Custom Converter : no action

 
Lou Caudell
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me re-post your code with the correct CODE tags so that the indentation stays. The CODE tags get created when you click the CODE button below the Add Reply button when posting to a thread

 
Lou Caudell
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's my answer for extending a converter in JSF.
An additional tld is not required.
Don't let the fact that your using a sun dataProvider to populate a table with strings from objects create confusion. Just implement the converter in appropriate components in your jsp.
- Lou Caudell

// -------------------------------------------------------- class HexConverter
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.*;


public class HexConverter implements Converter
{
public HexConverter() { }

// ========================================================== getAsObject(...)
public Object getAsObject( FacesContext facesContext, UIComponent uiComponent, String value )
{
Long longValue = new Long(-1);
try
{
longValue = Long.parseLong( value, 16 );
}
catch (Exception exception)
{
throw new ConverterException(exception);
}
return (new Integer(longValue.intValue()));
}

// ========================================================== getAsString(...)
public String getAsString( FacesContext facesContext, UIComponent uiComponent, Object obj )
{
String hexStr = new String();
try
{
int intValue = (int)((Integer)obj).intValue();
hexStr = Integer.toHexString(intValue);
}
catch (Exception exception)
{
throw new ConverterException(exception);
}
return hexStr;
} // method
} // class
 
Lou Caudell
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Continued.....

// ---------------------------------------------------------------------------------- JSP
<ui:tableColumn binding="#{Page1.tableColumn8}" headerText="mask" id="tableColumn8">
<ui:textField binding="#{Page1.textField5}" id="textField5" style="width: 50px" text="#{currentRow.value['backcolor']}" >
<f:converter converterId="HexConverter"/>
</ui:textField>
<ui:message binding="#{Page1.message5}" id="message5" showDetail="false" showSummary="true"/>
</ui:tableColumn>

// ------------------------------------------------------------------------------ faces_config.xml
<converter>
<converter-id>HexConverter</converter-id>
<converter-class>HexConverter</converter-class>
</converter>
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic