Dropdown Selection - Seam / Richfaces / JSF / xhtml

Dropdown Selection - Seam / Richfaces / JSF / xhtml

When developing JSF pages it is commonly required to associate the possible selections of a dropdown component with a list of objects or an enumeration of values.Unfortunately with standard JSF this requires quite a bit of glue code to achieve. Seam makes this simple through the tags. Even further when the tags are combined, you can directly associate JPA or Hibernate managed entities by simply binding a dropdown component to an entity association attribute.

Dropdown Tags

Dropdown Examples
The following example demonstrates directly associating a managed entity from a List of entities. The CreditCard class represents types of credit cards and the @NamedQuery allows us to load all CreditCard types from the data store.The Booking class is then be defined with a @ManyToOne reference to the CreditCard entity.

In order to load the list of credit card types, the Booking Action can define an @Factory method which initializes the list of CreditCard entities in the conversation context.

The method simply uses the named query we defined previously to load the entities from the current Entity-Manager instance. Note that our Seam-managed Persistence Context (SMPC) is named entityManager. If your SMPC is named something other than entityManager, you will have to configure the EntityConverter in components.xml (see the UI Namespace).

Finally, we can define a JSF component which simply binds directly to the creditCard attribute of the current booking instance.

The #{creditCardTypes} are loaded into the conversation context from the @Factory method we defined previously. The component allows us to iterate over the list of CreditCard entities and display the description by referencing the type variable. The tag ensures that the user selection is converted to an entity for association with the booking instance.


Post a Comment

Popular Posts