• Post Reply Bookmark Topic Watch Topic
  • New Topic

enum type in parametrized class

daniele licitra
Ranch Hand
Posts: 81
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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?
Tim Holloway
Posts: 18414
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • 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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!