wood burning stoves 2.0*
The moose likes JSF and the fly likes Using boolean value in SelectOneRadio Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "Using boolean value in SelectOneRadio" Watch "Using boolean value in SelectOneRadio" New topic
Author

Using boolean value in SelectOneRadio

Dylan Honorez
Greenhorn

Joined: Oct 02, 2006
Posts: 29
Hi, I'm facing a annoying problem for which I can't seem to find a simple solution.

The thing is that I want to use a SelectOne component to select true or false and use this selected value in my backing bean, pretty straightforward. I thought I read that a BooleanConvertor is used automatically, but this doesn't seem to be happening. I get conversion issues all the time: SelectOne.INVALID. Can anyone tell me what I'm understanding wrong?

This is the idea in my code:

Backing bean:


JSP:


[ April 07, 2008: Message edited by: Dylan Honorez ]
[ April 07, 2008: Message edited by: Dylan Honorez ]

SCJP - SCWCD
Yasir Bajwa
Greenhorn

Joined: May 02, 2007
Posts: 23
<h:selectOneRadio value="#{myBean.myBooleanOption">
<f:selectItem itemValue="#{true}" itemLabel="Yes" />
<f:selectItem itemValue="#{false}" itemLabel="No" />
</h:selectOneRadio>

Missing closing brace for myBean.myBooleanOption

Use just "true", "false" as item values
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15959
    
  19

In Java, a boolean is not an integer (unlike C or BASIC). So you can't feed a binary model to a control that expects integer values.

If there's a good display-only JSF control for booleans, I've missed it - for quick and dirty purposes, I use the read/write boolean checkbox and have the setter method ignore the input.

That knife cuts both ways, BTW. I've had to put up a facade for database ORM records for databases that have boolean fields but don't support boolean datatypes if the ORM system hasn't made the conversion for me.


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

Joined: Oct 02, 2006
Posts: 29
Apparently I'm missing something here. How is #{true} an integer?

Or, do you mean that a SelectOneRadio control always expects an Integer as value model? Can't you also use plain strings in a SelectOneRadio?
Dylan Honorez
Greenhorn

Joined: Oct 02, 2006
Posts: 29
Originally posted by Yasir Bajwa:
<h:selectOneRadio value="#{myBean.myBooleanOption">
<f:selectItem itemValue="#{true}" itemLabel="Yes" />
<f:selectItem itemValue="#{false}" itemLabel="No" />
</h:selectOneRadio>

Missing closing brace for myBean.myBooleanOption

Use just "true", "false" as item values


Thanks, but I was merely making an example. even with the closing brackets, my app doesn't work, that's my point.
Dylan Honorez
Greenhorn

Joined: Oct 02, 2006
Posts: 29
Also, I tried replacing


with:


and then adding

to my backing bean, with the same unwanted effect however.
Bob Good
Ranch Hand

Joined: Jan 09, 2008
Posts: 86
Can you use a checkbox instead? GUI wise I think it is more correct for a true false selection. It will talk properly to a backing bean boolean:
Dylan Honorez
Greenhorn

Joined: Oct 02, 2006
Posts: 29
Originally posted by Bob Good:
Can you use a checkbox instead? GUI wise I think it is more correct for a true false selection. It will talk properly to a backing bean boolean:


I would use one if I thought it was appropriate. But consider this case:
In my model I use a boolean to specify the 'ad hoc' property. The opposite is 'continuous'. Presenting this in a non-confusing way is the easiest with radio buttons or a dropdown menu. But the dropdown will only have the same effect.

I conclude that the selectItem lists don't handle booleans standardly. I'm going to try to use the supplied BooleanConverter class and set it explicitly.
Dylan Honorez
Greenhorn

Joined: Oct 02, 2006
Posts: 29
no such luck, adding

and then declaring the converter in the selectOneRadio converter attribute results in no conversion errors (that's good!), but passing through null values (that's bad!)
Bob Good
Ranch Hand

Joined: Jan 09, 2008
Posts: 86
Here is a working example with backing bean of selectOneRadio:

http://www.exadel.com/tutorial/jsf/jsftags-guide.html

Sample code can be downloaded from the site.
Dylan Honorez
Greenhorn

Joined: Oct 02, 2006
Posts: 29
Thanks, that helped clarifying ...

Conclusion: new SelectItem takes an 'Object value' as a parameter, but in fact never works unless the value object is a String.
Dylan Honorez
Greenhorn

Joined: Oct 02, 2006
Posts: 29
Actually, working the way the example does still doesn't solve the problem for me. The problem is, when I try to execute an action on the bean (form submission), the selected value doesn't bind back to the bean.
Dylan Honorez
Greenhorn

Joined: Oct 02, 2006
Posts: 29
Lol, it seems like my problems were caused with tags not being properly closed. Now I CAN map to any object.

Thanks for all the suggestions.
Bob Good
Ranch Hand

Joined: Jan 09, 2008
Posts: 86
lol from above:


Missing closing brace for myBean.myBooleanOption
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15959
    
  19

Originally posted by Dylan Honorez:
Thanks, that helped clarifying ...

Conclusion: new SelectItem takes an 'Object value' as a parameter, but in fact never works unless the value object is a String.


Actually, I'm pretty sure an app I've been working on has a droplist with integer values. However, the critical words there are "Object value". In other words, you can't use a primitive type like int, you have to make an Integer object out of it.
Dylan Honorez
Greenhorn

Joined: Oct 02, 2006
Posts: 29
Well, I found that you can use Integers to use as Object value, but to be able to handle them as Integers from the backing bean, you need to fill the control from within a backing bean. If you just use a SelectItem in your JSP and fill it with itemValue "2", you won't be able to submit your form.

My problems were caused by a combination of all of the above, without ever getting proper feedback from JSF or my application server.

So yes, the missing closing brackets were critical, but closing them at first didn't solve things because the Object Values were giving me a hard time. Hence the frustration.
[ April 21, 2008: Message edited by: Dylan Honorez ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using boolean value in SelectOneRadio
 
Similar Threads
Simple question on SelectOneRadio
How to select one Radio button by default.
How to diable a item in select option?
selectOneRadio in several columns...
HTML generated by backing bean