wood burning stoves 2.0*
The moose likes JSF and the fly likes Illegal Argument in Custom Converter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » JSF
Bookmark "Illegal Argument in Custom Converter" Watch "Illegal Argument in Custom Converter" New topic
Author

Illegal Argument in Custom Converter

brian lima
Greenhorn

Joined: Mar 29, 2010
Posts: 1
I'm attempting to create a custom converter that limits the amount of text displayed in an output field. I am currently getting an IllegalArgumentException - argument type mismatch, when the page loads which I cannot figure out. I've tried it with the argument as a String, int, and Integer with the same result. Here are the pieces:

tld:

<tag>
<name>limitTextDisplayedConverter</name>
<tag-class>com.med.txreq.jsf.converters.LimitTextDisplayedConverterTag</tag-class>
<attribute>
<name>maxCharsDisplayed</name>
</attribute>
</tag>



faces-config:

<converter>
<converter-id>limitTextDisplayed</converter-id>
<converter-class>converters.LimitTextDisplayedConverter</converter-class>
<property>
<description>Max number of digits displayed before being replaced with ...</description>
<property-name>maxCharsDisplayed</property-name>
<property-class>java.lang.String</property-class>
</property>
</converter>



Converter:

package converters;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

/**
* Will replace characters after MAX_CHARS_DISPLAYED with an elipsis (...)
*
*/
public class LimitTextDisplayedConverter implements Converter {
public static final String CONVERTER_ID = "limitTextDisplayed";
private static final String CONTINUATION_CHARS = "...";
private String maxCharsDisplayed;

@Override
public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
return null;
}

@Override
public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
String displayText = (String)arg2;
Integer maxChars = new Integer(getMaxCharsDisplayed());
if (displayText == null) return displayText;
if (displayText.length() < maxChars) return displayText;

StringBuffer sb = new StringBuffer(maxChars + CONTINUATION_CHARS.length());
int length = displayText.length();
if (length > 0 && length > maxChars)
{
sb.append(displayText.substring(0, maxChars));
sb.append(CONTINUATION_CHARS);
}
return sb.toString();

}

public String getMaxCharsDisplayed() {
return this.maxCharsDisplayed;
}

public void setMaxCharsDisplayed(String maxCharsDisplayed) {
this.maxCharsDisplayed = maxCharsDisplayed;
}

}




ConverterTag:

package converters;

import javax.faces.convert.Converter;
import javax.servlet.jsp.JspException;

import com.sun.faces.taglib.jsf_core.ConverterTag;

public class LimitTextDisplayedConverterTag extends ConverterTag {
/**
*
*/
private static final long serialVersionUID = -8976816060599681131L;

private String maxCharsDisplayed;

/**
* Default Constructor.
*/
public LimitTextDisplayedConverterTag() {
super();
init();
}

public void release() {
super.release();
init();
}

private void init() {
maxCharsDisplayed = "0";
}

//METHODS FROM ConverterTag
protected Converter createConverter() throws JspException {
LimitTextDisplayedConverter result = (LimitTextDisplayedConverter) super.createConverter();
assert (null != result);

result.setMaxCharsDisplayed(getMaxCharsDisplayed());

return result;
}



public void setMaxCharsDisplayed(String maxCharsDisplayed) {
this.maxCharsDisplayed = maxCharsDisplayed;
}

public String getMaxCharsDisplayed() {
return maxCharsDisplayed;
}




}




jsp:

<ice:outputText value="#{title}" title="#{title}">
<blima:limitTextDisplayedConverter maxCharsDisplayed="50"/>
</ice:outputText>




Am I missing something? Any help is much appreciated!
 
GeeCON Prague 2014
 
subject: Illegal Argument in Custom Converter