0

I am truncating a string if it is beyond a certain length. I'm super new to jsp, so I'm doing it the only way I know how. I have a bit of javascript that will create a new var with the truncated string. I need to use this new displayTag string within the html. However, I realized I can't use that var outside of the script tags. How can I achieve this?

 <script type="text/javascript">
    var displayTag = "${example.name}";
    if(displayTag.length > 15) {displayTag=displayTag.substring(0,15)+"..."};
    alert(displayTag); // for testing
 </script>

 <a href="some_link"><c:out value=displayTag/></a> // I know this line will not actually work
3
  • 2
    @RobG, that's not Javascript. That's EL, which gets interpreted by Java. When the browser eventually runs it, it will see a string of text and not ${example.name}. Even if it was just ${example.name}, the JS is still valid since ${example.name} is inside a string. Commented Jul 26, 2011 at 23:39
  • my issue is that I can't figure out how to get the length of this string: ${example.name}, because i can't just use example.name.length() like I could in normal java. Commented Jul 26, 2011 at 23:41
  • @Charlotte I've answered the question. fn:substring along with fn:length will let you do what you want. Commented Jul 26, 2011 at 23:46

2 Answers 2

1

fn:substring and fn:length are probably what you're after.

<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

<c:choose>
   <c:when test="${fn:length(example.name) > 15}">
      ${fn:substring(example.name, 0, 15)}...
   </c:when>
   <c:otherwise>
      ${example.name}
   </c:otherwise>
</c:choose>
Sign up to request clarification or add additional context in comments.

5 Comments

The JSTL functions are pretty useful :).
What if I wanted to append a string the result of substring? That isn't seeming to work for me...
This answer is appending to the result of the substring: within the <c:when> it puts the "..." after the closing }. (As an aside, I'm having trouble understanding why JSTL is better than the old-school <%= %> for this sort of thing, where this particular test could be done quickly and easily with a single line of Java instead of eight lines of JSTL.)
@nnnnnn scriptlets allow business logic to leak into the view layer (among other things). This is why their use is discouraged.
@Charlotte you don't need to explicitly concatenate. This example already does it for you.
0

Your ${example.name} is an EL expression using a JSP variable; displayTag is a Javascript variable, so as you say <c:out value=displayTag/> won't work because <c:out ...> runs on the server before the page is ever sent to the browser, so the javascript variable doesn't even exist.

I prefer to do this kind of thing on the server so it works even if Javascript is off or fails on an error.

An EL expression to do this could be

${fn:length(example.name) <= 15 ? example.name : fn:substring(example.name, 0, 15) + '...'}

(I haven't test that) The + operator doesn't work for string concatenation in EL, and there are no fn:concat or fn:append functions in standard JSTL, although concat does seem to be part of <%@ taglib prefix="x" uri="http://java.sun.com/jstl/xml" %>

In that case it would be

${fn:length(example.name) <= 15 ? example.name : x:concat(fn:substring(example.name, 0, 15), '...')}

That is, if the length of example.name is 15 or less just use it, otherwise take the substring and append "...", all done on the server as part of the JSP processing.

2 Comments

yeah, apparently + '...' doesn't work. I don't see any way of doing it with build it functions since there is no append jsp function
i will ask it in a separate question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.