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

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.

Related

How to deal with "transient" input elements that have no backing bean representation?

I have come across some situations in which input controls are used that do not have the value attribute set, i.e. they have no direct representation in the backing bean.
User input is handled entirely by ajax listeners. An example are the input elements of the column filters in the primefaces table.
However, when dealing with such input fields, MyFaces warns me about each:
Feb 09, 2017 3:04:51 PM javax.faces.validator.BeanValidator validate
WARNING: cannot validate component with empty value: my_form:myTableId:j_id_1s
In a way, this problem has already been mentioned here: p:datatable filter: cannot validate component with empty value
My question is: What is the best practice in situations like this?
An input element should always have a reference in the backing bean, everything else is a design error
Just use a "dummy" value that points to an unused field in the bean.
Suppress the validation of that input element. (How?)
Suppress the MyFaces warning somehow. (How?)
1. An input element should always have a reference in the backing bean, everything else is a design error
Not true.
2. Just use a "dummy" value that points to an unused field in the bean.
No. That's ridiculous in short term and confusing in long term at best.
3. Suppress the validation of that input element. (How?)
You could do. You can use <f:validateBean> for that.
<h:inputXxx>
<f:validateBean disabled="true" />
</h:inputXxx>
4. Suppress the MyFaces warning somehow. (How?)
Based on its source code there doesn't seem to be any way. You'd best ask them to log it only during Development stage.

Number alignment in JSF

I am developing an ADF based project. The code below is a part of my View. I want to show a NUMBER value in the input text field which can be changed anyway. All I want is that value to be right aligned in the field. I have read that contentStyle="text-align:right; does the job, but the requirement is not to use it. I am wondering myself if the value is of type number and convertNumber is used in the code, should the value be right aligned automatically?
I will appreciate any help you give me!
<af:inputText value="#{row.bindings.B.inputValue}"
styleClass="class" simple="true"
required="#{bindings.A.hints.B.mandatory}"
maximumLength="#{bindings.A.hints.B.precision}"
shortDesc="#{bindings.A.hints.B.tooltip}"
id="bb" autoSubmit="true"
disabled="#{viewScope.aBean.getSmth('abc')}"
valueChangeListener="#{viewScope.aBean.onBChange}">
<f:validator binding="#{row.bindings.B.validator}"/>
<af:convertNumber groupingUsed="false"
pattern="#{bindings.AA.hints.B.format}"/>
Use AFFieldNumberMarker in styleClass property of af:inputText, It will right align your value

JSF Multiple components in grid

I am trying to get the reusable group of jsf 1.2 components inside a panelgrid using Facelet tag file. #Balusc's previous answer at How to make a grid of JSF composite component? was a fabulous resource. I have a couple of followup questions:
In my c:when how do I test for the tagName itself instead of checking for the attributes. Instead of
<c:when test="#{type != 'submit'}">
I want to check tagName itself to decide how to format it. If 'input' do xxx.
2 Is this approach is still valid with jsf 1.2 other than f:ajax? If yes, can I replace with a4j:support...?
In my c:when how do I test for the tagName itself instead of checking for the attributes.
I'm not sure how this question makes sense. It sounds like that you're approaching something not entirely right. Do you maybe have copypasted exactly the same piece of code over multiple tag files? You should not do that. Make it a reuseable <ui:composition> or maybe <ui:decoration> instead which you compose/decorate in every tag file along with a fixed and unique <ui:param> value depending on the taglib file.
Is this approach is still valid with jsf 1.2 other than f:ajax? If yes, can I replace with a4j:support...?
Being able to create tag files is not necessarily specific to JSF, but to the view technology used, which is in this case Facelets. You can even do similar stuff in its predecesor JSP, see also this answer for an example: JSF 1.2 custom component from jsp:include It should work just fine in every JSF version supporting the view technology in question.
As to ajax support, it doesn't matter to the tag file what you're all doing inside the tag file. If you want and can use <a4j:support> then just do it.

use Label and Value of f:selecteItems with rich:combobox

How can i display Label from SelecteItem and use the value as a Key in rich:combobox? Jboss forums says it is not possible since the rich:combobox is designed for suggestion. Not really a replacement for h:selectOneMenu . is there any work around or alternative way?
PS: i tried using JSF converter it is modifying both value and label.
no.
use selectOneMenu.
add a4j:support as a child if you want ajax.

Why EL gives me the wrong object as parameter between parenthesis?

Here's the situation:
In a rich:dataTable in an a4j:form, i create a a4j:commandLink to select the values and pass it to the bean with the jboss el action syntax
action="#{bean.myaction(myparameter)}"
This works without problem.
But If I re-render the form to filter the datatable with an ajax call, when I select the value, it gives me the wrong results: the index from the selection, but the data from before the filtering.
Any ideas?
Thank you Zack for giving me the right solution in only 5 minutes.
I think passing parameter in the action between parenthesis is more elegant but, hey: this works. :)
Thank you a lot.
P.s. I'm editing the title too.
Try using:
<a4j:commandLink action="#{bean.myaction}">
<f:param name="myparameter" value="paramValue" />
</a4j:commandLink>
and then access that parameter in your action via the requestParameter("myparameter") through the FacesContext.
As a side-note, this isn't jboss EL, it's unified expression language (EL). It's just a feature of JSP/JSF in general, as specified by Sun.
In addition to the Zack's answer, I would say that if you need to extend the EL expressions in order to have the ability to call method with parameters, you can use the EL Functors library:
action="#{bean.myaction$[myparameter].action}"
Is your datatable populated using a Collection annotated with #DataModel ? If so try removing it from the context when filtering so that it gets re-requested.
eg.
//In filter method
Contexts.removeFromAllContexts("yourDataModelCollection");
Putting the dataTable in a <a4j:region> worked for me. This way, you can still use JBoss EL parameters.

Resources