brian lima

Greenhorn
+ Follow
since Mar 29, 2010
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by brian lima

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!
13 years ago
JSF