This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes JSF and the fly likes JSF2 Populate  SelectOneRadio with Enum values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "JSF2 Populate  SelectOneRadio with Enum values" Watch "JSF2 Populate  SelectOneRadio with Enum values" New topic
Author

JSF2 Populate SelectOneRadio with Enum values

Adrian Bastholm
Greenhorn

Joined: Feb 01, 2010
Posts: 9
Hi,
I'm trying to use the following to get a Yes/No value from the database, which is a custom Enum type:


The yes and no radio buttons are not set to either, it's just like I'm getting a null value. The same code works fine with a boolean value.
I'm not sure how and why this doesn't work. I tried this:
but I get a configuration exception - the class doesn't seem to be found.
Shouldn't the autoconversion work here, since it does so for boolean ?
What do I need to do to make this work ?
Adrian Bastholm
Greenhorn

Joined: Feb 01, 2010
Posts: 9
I added a custom converter:


but it doesn't seem to have any effect
Adrian Bastholm
Greenhorn

Joined: Feb 01, 2010
Posts: 9
I got it one step further, now I actually get the converter to work and put values in the selectItem:

and the method from the controller:

and now the page source after rendering:

The only thing remaining now is how to make one of the two radios checked depending on the value of the expression:

Anybody ?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16022
    
  20

Actually, I think you're getting too clever. I'll admit that JSF and enums is a bit of a gray area to me, but the impression I've been getting (and this includes JSF 1.2) is that if the data type is an enum, the EL can handle it directly. In other words:


In other words, don't qualify the enumeration value. EL should deduce it from the target datatype.

You also of course, need set/get methods on the target that take actual enum datatypes and not numeric or display text values.

I'm basing this on my experience with SelectOneMenu, so YMMV. I'm also not totally sure about the itemValue. Since it's EL, I think you need the "#{}" parts, but if it doesn't work, try 'itemValue="YES"' just for giggles.


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

Joined: Feb 01, 2010
Posts: 9
and

both give give:


which is the same outcome, and You're right, the EL resolves the custom Enum even without qualifying it, but It's probably using the built-in EnumConverter (or no converter at all), and what I get is empty strings (or null). Doing it with a method returning a YNValue[] array which contains the YES and NO enums seems to engage the custom converter I wrote, and at least populate the itemValue fields(?) correctly.
Nevertheless, I still don't know how to check the "correct" radio button according to the value in the database
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16022
    
  20

I may be getting myself confused here, but there's a couple of items worth considering. First, of course is that just because radio buttons have only on/off settings doesn't mean that they're binary data type interfaces. You can, after all, set up for example three buttons in a group. Say ABORT, RETRY, CANCEL. The setter for the group's value would then be invoked with the enum value for whichever of the three radio buttons had been clicked to the ON position. Which is why you can't tie a binary-argument setter directly to a radio button. JSF is rather strict about such things - I've had grief with checkboxes for much the same reason.

So much for the presentation side. On the persistence side there's also room for trouble. Unfortunately, many databases don't support a native boolean datatype, so people have to fake it with character or integer fields with values like "Y/N", "T/F", "0/1" and so forth. Since there's no standard, there's no Java-level support, although if you're using an ORM, you may be able to configure in a data converter or conversion rule (which is usually called an "enumeration" value, even when it really isn't).

Stuff like this makes it a nuisance to try and present the actual ORM record directly to the UI. I usually end up putting a decorator/fa├žade in front of the actual record. Or you can simply write an independent property and bounce its value through to the record, converting from enum to boolean as you do so.

When converting enumerations to/from numeric and string representations, don't forget that enum has a number of convenience methods such as the ordinal() function.
Adrian Bastholm
Greenhorn

Joined: Feb 01, 2010
Posts: 9
Well for starters I'm having difficulties understanding the first part, and for the second part, You might have been right. Today I had a lot of strange exceptions and I dumped the old database, and created a new file, and reimported the data. This should be done by default by the cleanup script, because I believe that it was at least part of the problem. I use an object database, so binding the object properties directly to the UI components should not be a problem here.

One of the things I noticed was that you should (at least it looks like you should) always use a SelectItem[] array when populating a selectOneRadio with values from enums, even though it takes whatever array you're throwing at it, such as a MyCustomEnum.values() . It seems like the UI needs these SelectItems to do the initialization work properly. A SelectItem is a SelectItem(Object x, Object x.toString()), so in my case it's the Enum, and it's toString()
The same radiobuttons work well with boolean values without any SelectItem arrays, you can just use fix data as in my first post.

The method from the controller should not look like:

but like:
Thanks for trying to help me anyway

Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16022
    
  20

If the first part about Radio buttons not being Boolean values doesn't make sense, that's where a lot of your problems come in.

You don't need to create a SelectItem collection in your sample, because it's being created when the SelectItem tags are compiled.

However, your original example MUST have the following method defined in order to work right:


If you don't accept an enum argument, the framework won't function properly. If internally you have a boolean, you'd need something like this:


For best results, also test for non-null on the argument named "value" to avoid potential NullPointerException's.
Adrian Bastholm
Greenhorn

Joined: Feb 01, 2010
Posts: 9
OK, now that you simplified the first part, I get it, but they seem to work better with boolean values, probably because of the built-in BooleanConverter.
I have setters for all the enum values, as You suggest. I even generate all the getters and setters to make sure I stick to the naming conventions. About the SelectItem array, I'll have to go back and retry that, I'm not 100% sure - but things started working after that (and recreating the database)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JSF2 Populate SelectOneRadio with Enum values