Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ellipsis (abbreviate text) with JSP / EL

Tags:

java

jsp

el

taglib

I was wondering what is the best way to implement an ellipsis abbreviation with JSP / Expression Language.

For now, I've been using fn:substring, which is ok, but I would like to have the three dots "...", in case the text was truncated.

With a web search I found that Java Web Parts has an AbbreviateTag. However I was wondering if there are better libraries, or if it's better I roll my own custom tag. What do you suggest?

like image 810
stivlo Avatar asked May 21 '11 05:05

stivlo


2 Answers

The MMBase tag library has a tag that supports ellipsis.

However, implementing your own could be a good option ... depending on exactly how you expect the ellipsis to work.

like image 94
Stephen C Avatar answered Oct 20 '22 00:10

Stephen C


Since I couldn't understand how to use mmbase, I built my own custom tag extending SimpleTagSupport.

It works in this way:

<%@ taglib prefix="sti" uri="/WEB-INF/tlds/stivlo.tld" %> 
<p><sti:ellipsis>What a beautiful day.</sti:ellipsis></p>
<p><sti:ellipsis maxLength="10">What a beautiful day.</sti:ellipsis></p>

Output:

What a beautiful day.
What a bea…

I've also documented the custom tag implementation on my blog. This is my first custom tag, if anything can be done better, I'd be happy to hear from you.

like image 27
stivlo Avatar answered Oct 19 '22 22:10

stivlo