This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JSF and the fly likes How to write boolean expression from string in JSF? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "How to write boolean expression from string in JSF?" Watch "How to write boolean expression from string in JSF?" New topic
Author

How to write boolean expression from string in JSF?

Dave Alvarado
Ranch Hand

Joined: Jul 02, 2008
Posts: 436
Hi,

I'm using JSF 1.1 with Resin 3.0.19. Is there a way to write an equalsIgnoreCase expression in JSF syntax? I want to evaluate whether the expression is equal to any form of the word "true", below ...

<h:selectBooleanCheckbox value="#{emailController.fields.addToENews}" />

The expression, 'emailController.fields.addToENews' returns a string, but the selectBooleanCheckbox requires a boolean.

Thanks for any help, - Dave
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15950
    
  19

Resin, Tomcat, WebLogic, doesn't matter.

In some cases, you can map Strings directly to booleans, since Java has certain conversion conventions (and I believe they're case-insensitive), but I'm assuming that that didn't work here. Besides, I've fought the same problem myself, since databases rarely support true boolean datatypes.

You can create a custom JSF converter to handle this. One side of the conversion should be Boolean, the other would be String. Which on reflection, should be a String value of "true" or "false", I think, so now I'm not so sure if you've tried the easy solution or not.


Customer surveys are for companies who didn't pay proper attention to begin with.
Abhishk Gupta
Greenhorn

Joined: Oct 08, 2008
Posts: 22
Hey Dave, what exactly you are trying to get by using a string type for a checkbox. A checkbox needs a boolean so that it can flip the values on check-uncheck.
Anyways by the time you decide the correctness of the approach, answer of your question is, YES...
We can write expression to evaluate it.

<h:selectBooleanCheckbox value="#{emailController.fields.addToENews eq 'enews'}" />


Hope this helps


There are only 10 types of people in the world: those who understand ternary, those who don't, and those who mistake it for binary.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15950
    
  19

Welcome to the JavaRanch, Abhshisk!

You forgot one minor detail. One of the differences between "#" and "$" EL expressions is that the "#" can work in 2 directions. Consider what your sample would think of an attempt to update the backing bean.
Viswanath Tg
Greenhorn

Joined: Nov 27, 2007
Posts: 20
You can use boolean expressions as follows

oncomplete="if(#{myObj.isSelected})Richfaces.hideModalPanel('popup1');"


Helping hands are better than praying lips
Abhishk Gupta
Greenhorn

Joined: Oct 08, 2008
Posts: 22
Thanks Tim

I understand your concern. That is the reason why I asked Dave to check his approach coz in value tag you are not supposed to evaluate expressions. This sample was just to demonstrate the use of expression language in JSF. This can be useful when your are using attributes like 'rendered' , 'disabled' etc.
Correct me if I am wrong.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15950
    
  19

Abhishk Gupta wrote:Thanks Tim

I understand your concern. That is the reason why I asked Dave to check his approach coz in value tag you are not supposed to evaluate expressions. This sample was just to demonstrate the use of expression language in JSF. This can be useful when your are using attributes like 'rendered' , 'disabled' etc.
Correct me if I am wrong.


Oh, I've a few of those myself. But the boolean checkbox is an input/output control, which is why I mentioned the perils of using a non-lvalue expression on its "value" attribute.
 
Consider Paul's rocket mass heater.
 
subject: How to write boolean expression from string in JSF?
 
Similar Threads
tag inside other tag
Assertion
Expression instead of condition in if block
Boolean Expression Evaluation Framework
Boolean type from 0 length string