• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

[JSF 2.0] f:selectItems with custom class

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic