3 Replies Latest reply on Mar 25, 2007 10:37 AM by henrik.lindberg

    How to truncate output?

    henrik.lindberg

      Hi,
      I would like to be able to truncate output of certain properties. I have a property called "description" that can be quite long, and in listings I want to limit the output to say 40 characters and then add "..." after the truncated text.

      I would like to be able to do this in the view.
      I looked at the Converter interface, but I am not sure that creating a Converter is the best option.

      Any suggestion on how to do this?

        • 1. Re: How to truncate output?
          christian.bauer

          http://jira.jboss.com/jira/browse/RF-27

          Until then:

          public class WikiUtil {
          
           public static String truncateString(String string, int length, String appendString) {
           if (string.length() <= length) return string;
           return string.substring(0, length-1) + appendString;
           }
          }
          


          wiki-taglib.xml

          <?xml version="1.0"?>
          <!DOCTYPE facelet-taglib PUBLIC
           "-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN"
           "http://java.sun.com/dtd/facelet-taglib_1_0.dtd">
          
          <facelet-taglib>
           <namespace>http://jboss.com/products/seam/wiki</namespace>
          
           <function>
           <function-name>truncateString</function-name>
           <function-class>org.jboss.seam.wiki.util.WikiUtil</function-class>
           <function-signature>java.lang.String truncateString(java.lang.String,int,java.lang.String)</function-signature>
           </function>
          
          </facelet-taglib>
          


           xmlns:wiki="http://jboss.com/products/seam/wiki"
          
           #{wiki:truncateString(aString, 20, '...')}
          




          • 2. Re: How to truncate output?
            atao

            You can use jstl functions:

            xmlns:fn="http://java.sun.com/jsp/jstl/functions"
            
            [...]
            
            <h:outputText value="#{fn:substring(aString, 0, 5)}..."/>
            


            • 3. Re: How to truncate output?
              henrik.lindberg

              Great - thanks!