Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to write boolean expression from string in JSF?

 
Dave Alvarado
Ranch Hand
Posts: 436
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 18152
52
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Abhishk Gupta
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18152
52
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use boolean expressions as follows

oncomplete="if(#{myObj.isSelected})Richfaces.hideModalPanel('popup1');"
 
Abhishk Gupta
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 18152
52
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic