• Post Reply Bookmark Topic Watch Topic
  • New Topic

Trimming a string in JSF

 
John Footh
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was wondering if there is a way to trim a string that is too long for the display using JSTL functions. So, for example if I had this:

My Really Long Display Name

and for purposes of the UI, I need to trim the string if it's greater than, say 20 characters to this:

My Really Long Disp...

I know I could easily do this in java, but what if the UI changes and the new length becomes 15 characters. I'd have to change java code for a view change which I'd like to avoid.

I was thinking I could extend the outputText control, but I was wondering if there was an easier solution.

Thanks!

-JF
 
Brendan Healey
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You can do it like this:



I would say that it would be better to do this in CSS, however I think there are
still cross browser compatibility issues with the various solutions out there, so
this is a solid way of doing it.

It's surprisingly easy to write your own EL functions, if you google "JSF EL extend
concat" you should find a few examples.

Regards,
Brendan.
 
John Footh
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Brendan, based on your advice, I am going to create a custom function that will chop the string (and add the ellipsis) that can be called from the view.
 
Tim Holloway
Bartender
Posts: 18412
58
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Uh, yeah.

You know, usually I adjust the display to fit the backend, not the other way around. Most often because the data ends up in a database with a maximum field size defined in the database schema.

HTML is capable of defining input text controls with both a display width and a data width. The display width is what actually shows up on-screen, and is either computed based on the "size" attribute or a CSS width. The data width (maxlength) ensures that people can't enter data that's bigger than the backend of the system can handle. If there's more data (text) that can fit in the "size" of the control, the display is trimmed, but the full extent of the data remains so that no data is actually lost just because the UI can't make it all fit in the display area (which is scrollable).

These days a lot of people use validation extensions (typically annotations) to enforce size limits on the Java code, but that only kicks in after the data is submitted, so I prefer to enforce it in the View definition in addition, and for the most part, I consider the maintenance to be just the price of doing business.

If you really, really want "one-stop shopping", you can always expose read-only widht/maxlength properties on your backing bean and code the inputText attributes to reference them as EL expressions.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!