Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSF 2.0: Limit value of h:outputtext to a specific number of characters

Tags:

jsf

jsf-2

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.

like image 720
Justin Kredible Avatar asked Mar 26 '12 14:03

Justin Kredible


3 Answers

You can use JSF EL.. for example

<h:outputText value="#{bean.string.length() gt 50 ? bean.string.substring(0,47).concat('...') : bean.string}" />
like image 173
YAM Avatar answered Oct 26 '22 09:10

YAM


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>
like image 7
Haroldo_OK Avatar answered Oct 26 '22 08:10

Haroldo_OK


Use Custom JSF Converters as explained in "JSF for nonbelievers". Here's an example of text truncation converter.

like image 4
Boris Pavlović Avatar answered Oct 26 '22 08:10

Boris Pavlović