File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using boolean value in SelectOneRadio

 
Dylan Honorez
Ranch Hand
Posts: 30
AngularJS Mac OS X Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Yasir Bajwa
Greenhorn
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<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
Pie
Posts: 17622
39
Android Eclipse IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dylan Honorez
Ranch Hand
Posts: 30
AngularJS Mac OS X Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 30
AngularJS Mac OS X Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 30
AngularJS Mac OS X Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, I tried replacing


with:


and then adding

to my backing bean, with the same unwanted effect however.
 
Bob Good
Ranch Hand
Posts: 86
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 30
AngularJS Mac OS X Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 30
AngularJS Mac OS X Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 86
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 30
AngularJS Mac OS X Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 30
AngularJS Mac OS X Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 30
AngularJS Mac OS X Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 86
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lol from above:


Missing closing brace for myBean.myBooleanOption
 
Tim Holloway
Saloon Keeper
Pie
Posts: 17622
39
Android Eclipse IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 30
AngularJS Mac OS X Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic