EL does not know a string concatenation operator. Instead, you would just inline multiple EL expressions together. The +
operator is in EL exclusively a sum operator for numbers.
Here's one of the ways how you could do it:
<c:set var="tooLong" value="${fn:length(example.name) > 15}" />
${tooLong ? fn:substring(example.name,0,14) : example.name}${tooLong ? '...' : ''}
Another way is to use an EL function for this wherein you can handle this using pure Java. For an example, see the "EL functions" chapter near the bottom of my answer in Hidden features of JSP/Servlet. You would like to end up as something like:
${util:ellipsis(example.name, 15)}
with
public static String ellipsis(String text, int maxLength) {
return (text.length() > maxLength) ? text.substring(0, maxLength - 1) + "..." : text;
}
fn:substring(example.name,0,14) + '...'
? I've always found Java's ternary operator to be finicky – Susiexample
'sgetName()
function. And I wonder why this thread has the [javascript] tag? I think an [el] tag should be here instead of the [javascript] tag – Glasshouse${cond1 ? .. : cond2 ? ... : ...}
. The Apache EL parser (which is the most widely used one) has indeed quirks with this. There's in this particular case however no means of multiple conditional expressions. – Cyclist