2 Replies Latest reply on Jan 20, 2009 12:36 AM by www.supernovasoftware.com

    How to truncate a string

    fvalente

      Hi,


      I wanna known if there is a way to truncate a string and and dots in the end, eg:'This is a very long text...'.


      I searched on Google and on Seam documentation and I didn't find.


      Is there an attribute for any tag that does this or I have to do it manually by creating a method in my entity bean?

        • 1. Re: How to truncate a string
          malex

          1)
          Checkout the Apache Commons Lang (StringUtils.abbreviate(java.lang.String str, int maxWidth)) or write yourself the method.


          2)
          In order to directly access your method in the frontend (facelets), you can create an EL Function. The referece demonstrates, how you can very easily include the static methods in an ELFunctions library (automatic dynamic registration).


          • 2. Re: How to truncate a string
            www.supernovasoftware.com

            The java method would be trivial.


            I would use a combination of rich:toolTip and only render it if the text is abbreviated.


            This way the user could mouse over and see the full text.


            Combining a Seam component and Facelets template should work well.


            Or use Richfaces CDK.


            It would be nice to have


            <custom:abbreviateText value="my text" length="10" />