13

I have a string which can be yes or no, instantiated in an object in a Java backing bean. I can't seem to find the best way to conditionally style the text red or green dependent on whether the JSF gets yes or no from the bean respectively. I'm using richfaces, but should I be using <c:if> tags?

volvox
  • 3,014
  • 16
  • 51
  • 80

1 Answers1

23

(in order of preference):

  • style="color: #{yourVar == 'yes' ? 'green' : 'red'};"
  • make two <h:outputText> components with different styles, each with a different rendered attribute (one #{yourVar == 'yes'} and the other #{yourVar == 'no'})
  • define a (jstl/facelets/jsf 2.0) function that takes the var as argument and returns a style/class - styleClass="#{my:getStyleClass(yourVar)}"
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • 2
    IMHO, better is a method that returns the class name or empty string so: – Luke Feb 08 '10 at 17:41
  • 5
    @Luke, IMHO that's only more clutter in the bean. It doesn't need to know anything about the view-specific stuff. An EL ternary condition in `style` or `styleClass` (option 1 by Bozho) makes perfectly sense here. – BalusC Feb 08 '10 at 17:46