How can i make outputLabel value change according to a condition?

1.3k Views Asked by At

I have the following outputLabe code in jsf:

<ice:outputLabel value="#{litApp.TipusTramitImportAtorgat}" rendered="#{tipusTramitBB.detailEntity.id == 12}"/>

It appears only if tipusTramitBB.detailEntity.id == 12, if not it must have an other value, is there anyway to do it in this same outputLabel or i have to add an other outputLabel which appears if tipusTramitBB.detailEntity.id != 12

2

There are 2 best solutions below

0
On BEST ANSWER

It can be done in one label:

<ice:outputLabel value="#{tipusTramitBB.detailEntity.id == 12 ? litApp.TipusTramitImportAtorgat : litApp.otherValue}"/>
0
On

Though 2 labels with rendered attribute could have the same semantic, it is better to have the only element with same behavior for some reasons: clearer code and thus better maintenance, ajax rerendering, etc.

One solution is (as @Geinmachi mentioned) using of ternary operator:

<ice:outputLabel value="#{tipusTramitBB.detailEntity.id == 12 ? litApp.TipusTramitImportAtorgat : litApp.otherValue}"/>

Another solution (and I prefer this one) is to put your business logic direct in the managed bean (litApp in your case), something like:

public class LitApp {

    public Object getTipusTramitImportAtorgat() {
        if (tipusTramitBB.getDetailEntity.getId() == 12) {
            return "Value1";
        } else {
            return "Value2";
        }
    }
}

You have to get access to the tipusTramitBB bean from the litApp, but this is another question.

This is a general approach, but is often preferable, especially if you have a more complex business logic. Rendered attribute has another meaning, it designed for occasional hiding of element while rendering.