TurboManage

David Chandler's Journal of Java Web and Mobile Development

  • David M. Chandler


    Web app developer since 1994 and former Developer Advocate with Google now residing in Colorado. Besides tech, I enjoy landscape photography and share my work at ColoradoPhoto.gallery.

  • Subscribe

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 241 other followers

  • Sleepless Nights…

    May 2006
    S M T W T F S
    « Mar   Aug »
     123456
    78910111213
    14151617181920
    21222324252627
    28293031  
  • Blog Stats

    • 876,139 hits

JSF Tip of the Day: EL Conditional Operator

Posted by David Chandler on May 24, 2006

Do you ever need to convert a boolean value for output to Yes/No (in any language) as opposed to the default true or false that JSF renders? Here’s a nifty way to do it with a properties file and the JSF EL conditional operator. Create a properties file with keys for “true” and “false”. Example:

true=Yes
false=No

Then, in your view

<f:loadBundle basename=”path.to.your.properties.file” var=”msgs” />

<h:outputText value=”#{ (myBean.someBoolean==true) ? msgs[‘true’] : msgs[‘false’] }” />

Nifty, huh?

/dmc

Advertisements

7 Responses to “JSF Tip of the Day: EL Conditional Operator”

  1. Anonymous said

    You can make that a bit “niftier” by doing:

    < h:outputText value=”#{ (myBean.someBoolean) ? msgs.true : msgs.false }” / >

    However, better JSF practise states that you should be using a converter:

    < h:outputText value=”#{myBean.someBoolean}” converter=”booleanConverter” / >

    Which is a more elegant and portable solution anyway.

  2. What is the solution if you have n- possible values of an argument? For example “yes /no /don’t konw/ don’t care”. It is like switch-operator.

    Tsvetan

  3. steve88 said

    Thanks

  4. Pahan said

    well it depends on usability if you want to use this to display Boolean as string this will do but if you want to convert back their selection to boolean then

    makes best practise.

  5. Stroke said

    Hi.

    How could i have something like this:

    ?????? I did but an error was displayed

    Thanks!

  6. Stroke said

    Sorry, the text wasn’t displayed…. it’s the idea:

    h:outPutText value=”#{(myBean.someIntAttribute==0)? msgs.true : #{myBean.someIntAttribute}}

    thanks.

  7. Stroke said

    Well… i solved it….if someone needs the solution:

    h:outPutText value=”{(myBean.someIntAttribute==0)? msgs.true : (myBean.someIntAttribute)}”

    i hope this helps someone..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: