Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

[JSF 2.0] f:selectItems with custom class

 
Mike Monge
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:


Product.java:


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
Posts: 18303
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic