2

In my application, there is a field shown on the jsp. I wrote it by using

<td>
<c:out value="${field}" />
</td>

However, I dont want everything in that value(field) to be shown. Only first 15 characters should be shown. How can I do that? I am using spring 3.1 and it is amven project. Thanks,

Kevin Swann
  • 1,018
  • 12
  • 28
kaan
  • 51
  • 1
  • 9

3 Answers3

1

You can use the jstl substring function as described in previous answers, JSP String formatting Truncate

Community
  • 1
  • 1
Romski
  • 1,912
  • 1
  • 12
  • 27
1

Use this:

<c:choose>
   <c:when test="${fn:length(field) > 15}">
      ${fn:substring(field,0,10)}...
   </c:when>
  <c:otherwise>
     ${field}
  </c:otherwise>
</c:choose>

You can use to display your output, I have written here directly .

Shoaib Chikate
  • 8,665
  • 12
  • 47
  • 70
  • If you have time, I posted another question but I have not take any answer yet. The url is : http://stackoverflow.com/questions/19764992/using-ckeditor-it-does-not-work. Thanks for advance. – kaan Nov 04 '13 at 15:27
  • It is not necessary to check for the string length - JSTL substring() handles this. – Kevin Swann May 14 '18 at 15:27
1

All that is needed is:

  ${ fn:substring( field, 0, 15 ) }

It is not necessary to check the string length as the JSTL fn:substring() function handles if it is passed a shorter string than the required length.

<c:set var="string10" value="1234567890"/>
<br/>string         : ${ string10 }
<br/>substring 0..10: ${ fn:substring( string10, 0, 10 ) }
<br/>substring 0..20: ${ fn:substring( string10, 0, 20 ) }

gives:

string         : 1234567890 
substring 0..10: 1234567890 
substring 0..20: 1234567890 

Note that this is different behaviour to core Java String.substring(..).

<br/>substring 0..20: ${ string10.substring( 0, 20 ) }

gives a:

StringIndexOutOfBoundsException
Kevin Swann
  • 1,018
  • 12
  • 28