7

I`m using JSF and I have an output text per example "qwerty" that I want to display / format differently if some conditions will be met. So per example if:

  • cond1 is true then I want to display qwerty
  • cond1 is false then I want to display qwerty

and so on.

Is there a way?

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
CyberGriZzly
  • 379
  • 3
  • 9
  • 22

4 Answers4

17

You can use a ternary operator in order to choose the style you want to apply depending on the condition:

<h:outputText value="qwerty" 
    style="#{backingBean.cond1 ? 'text-decoration:line-through;':'fontstyle:italic;'}">

And it would be even better if you use css classes in order of inline styles. Good luck!

Using the 'condition' inline in the EL is also possible, see using greater than logical expression in rendered attribute. Read also the discussion about using inline in the xhtml or in the javabean

Community
  • 1
  • 1
Aritz
  • 30,971
  • 16
  • 136
  • 217
  • I thought of that but I thought it is a better more elegant way. Thank you for your answer! – CyberGriZzly Mar 13 '13 at 21:16
  • That is not the most elegant way. You actually create two components in component tree. Also keep [DRY](http://en.wikipedia.org/wiki/Don%27t_repeat_yourself)! – Piotr Gwiazda Mar 13 '13 at 21:18
  • It's just a way to give a basic explanation about how conditional rendering-evaluation works in JSF. My second approach is better ;-) – Aritz Mar 13 '13 at 21:23
7

Neat way is to create CSS classes

<h:outputText value="qwerty" 
     styleClass="#{backingBean.cond1 ? 'classA' : 'classB'}" /> 
Piotr Gwiazda
  • 12,080
  • 13
  • 60
  • 91
6

In similar situations, I use following

<h:outputText value="yourValueFromYourBeanOrWhatever" styleClass="anArbitraryName#{managedBean.condition}"/>

And in my CSS file I define the classes:

.anArbitraryNametrue{}

and

.anArbitraryNamefalse{}
Laabidi Raissi
  • 3,263
  • 1
  • 22
  • 28
0

With the conditional. Before the interrogation you put the condition. If true, it returns what is before t ":". If it is false, it returns what is behind ":".

<h:outputText value="#{condition ? 'true' : 'false'}" />
luisja19
  • 27
  • 5