I'm using an h:outputText to display a value. I would like to limit the value displayed such that when it's greater than 50 characters in length, it's truncated to 50 characters and an ellipsis is appended to the truncated value. For example:
This is some text that has been trun...
I know I can achieve this in the backing bean but I was wondering if anyone knew of any tags that can achieve this without adding code to the managed been. Seems like a common use case.
Thanks.
You can use JSF EL.. for example
<h:outputText value="#{bean.string.length() gt 50 ? bean.string.substring(0,47).concat('...') : bean.string}" />
As BalusC mentioned, OmniFace's of:abbreviate() could be a good solution for your problem:
<p>Abbreviate a long string: #{of:abbreviate(string1, 20)}</p>
Use Custom JSF Converters as explained in "JSF for nonbelievers". Here's an example of text truncation converter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With