File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSF and the fly likes [JSF 2.0] f:selectItems with custom class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "[JSF 2.0] f:selectItems with custom class" Watch "[JSF 2.0] f:selectItems with custom class" New topic

[JSF 2.0] f:selectItems with custom class

Mike Monge

Joined: Sep 29, 2010
Posts: 1
I can't get f:selectItems to work with a custom class.

I tried several different ways to make it work, but I get validation errors on submit (like "invalid value").

I also tried writing a converter and it works, but I still get that validation error on submit.

If I have something like this:

What should the backing bean be like?

Ideally, testBean.selectedProduct should be a Product. I thought JSF 2.0 let you do this easily.

I tried making testBean.productList be a List<Product> and a List<SelectItem> and I always have the same problem.

Thanks a lot.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17410

The key to understanding this problem is to realize that you're generating HTML and sending HTML back when you submit.

You want the Select to select an binary object (class instance). But HTML is not a binary format. It's a text format. So you can't send out binary objects and expect to get them back. Not that I recommend doing so. Anything that goes from server to client and back can be hacked, so generally internal data structures shouldn't be.

What you can do is make the value part of the select be an identifier that's used to resolve the class object. A common usage is to use the key value for a database row and have the request processor look up that key. Or you could store the objects in a session-scope hash map, if you prefer.

A converter doesn't help here in any case, since what you actually supply to the control is a SelectItem collection, not the object itself.

An IDE is no substitute for an Intelligent Developer.
I agree. Here's the link:
subject: [JSF 2.0] f:selectItems with custom class
It's not a secret anymore!