Ellipsis (abbreviate text) with JSP / EL
Asked Answered
S

3

6

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?

Syl answered 21/5, 2011 at 5:43 Comment(3)
Wouldn't it be better to do this with CSS?Ocrea
It seems that text-overflow:ellipsis doesn't work in Firefox, or do you know another way?Syl
ah, you appear to be right - there do seem to be hacks and a jQuery plugin, but that is less than ideal.Ocrea
S
2

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.

Syl answered 22/5, 2011 at 5:22 Comment(1)
I am accepting my own answer, because I could not get mmbase to work.Syl
S
3

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.

Sextain answered 21/5, 2011 at 6:3 Comment(1)
I tried using mmbase, I've put mmbase-1.9.5.jar (the latest release) inside my WEB-INF/lib directory, but I've the error: "Can not find the tag library descriptor". I've also tried to add it to eclipse build path. I tried with uri "mmbase.org/mmbase-taglib-2.0" and -1.0. I guess I will try to implement my own for now.Syl
S
2

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.

Syl answered 22/5, 2011 at 5:22 Comment(1)
I am accepting my own answer, because I could not get mmbase to work.Syl
C
1

Old thread, but I am posting this for anyone searching for an answer. Below is how I handled this. The code was inside a loop creating td elements, hence the reference to columns. If the text needs truncation I adjusted the substring allowing room for the ellipsis and then appended the ellipsis.

<c:if test="${columnMaxLength ge 0}">      
  <c:set value="" var="ellipsis" />           
  <c:if test="${fn:length(colTxt) gt columnMaxLength}">     
    <c:set value="..." var="ellipsis" />    
  </c:if>
  <c:set value="${fn:substring(colTxt, 0, (columnMaxLength - fn:length(ellipsis)))}${ellipsis}" var="colTxt" />
</c:if>
<c:out value="${colTxt}" />
Crawfish answered 31/7, 2015 at 14:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.