• 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

enum type in parametrized class

 
Ranch Hand
Posts: 82
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi to all.

I'm developing a JSF application.

I have some enums, for example


I want to put the enum value in <h:selectOneMenu>. I use a bean:


and the xhtml is


It works!

I have a lot of enum to present in a combo box/ radio botton / select menu, so I need a bean for each enum...

There is a way to create an abstract parametric superclass like this?


How can i call the method .values() of enum for a generic enum T, like in the striked code?
 
Saloon Keeper
Posts: 27752
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
I'm not sure if I understood that, but the selection list for selectOneMenu and its relatives consists of an ordered collection of SelectItem objects. A SelectItem encapsulates a name/value pair corresponding to the VALUE= and body text values of an HTML OPTION element.

I think that in JSF2, you can define an h:selectItems element that is based on a raw list of text strings (like you're doing), but since the actual mechanism requires SelectItems, what really occurs is that JSF builds up the SelectItems automatically, but it relies on a a lot of defaults, so it's often insufficient.

What I recommend is this:

1. Define a backing bean in application scope to hold the SelectItem list. I'm presuming that this list is something that all users of the app will be using the same copy of. So build it once and share it forever.

2. Define a method to initialize that bean's SelectItem list. It can be a @PostConstruct method or you can do it on the first "getSelectItems()" method invocation and store it for future invocations.

When I say "list", of course, an array is also allowable, and somewhat better, since an enum has a fixed number of elements, so you don't need List's resizability features (or overhead).

And yes, it should be possible to define a class that accepts an enum type as a parameter, as far as I can deduce without doing something tedious like reading the manual. :)

Request scope, incidentally, is almost 100% useless in JSF. In particular, it will give nothing but grief for JSF UI sub-model objects such as DataTable and SelectItems.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic