• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Custom Converter : no action

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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>
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic