• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Illegal Argument in Custom Converter

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Squanch that. And squanch this tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic