02 September 2009

Convert entity to selectItem in Seam

When using entity classes and Hibernate to display data in JBoss Seam, you can easily convert the entity to a selectItem element for use in a dropdown box, radio button group, or checkbox items. Here's an example:
<h:selectOneMenu >
<f:selectItem itemLabel="" itemValue="#{null}"/>
<s:selectItems var="tape" itemValue="#{tape.description}"
value="#{tapeformatList.resultList}" label="#{tape.description}" />
<s:convertEntity/>
</h:selectOneMenu>

Note the null element to allow users to leave the selection blank. You can also achieve this by using the noSelectionLabel attribute. Also of note is the itemValue attribute on the Seam selectItems tag: This enables a custom value instead of the automatic numeric ID generated by Seam. In this case, the entity's description is used for the value.

Here's what this code generates in HTML:
<select name="tapeformatSearch:j_id14" size="1">    
<option></option>
<option value="VHS">VHS</option>
<option value="DVD">DVD</option>
<option value="Beta-SP">Beta-SP</option>
<option value="Digi-Beta">Digi-Beta</option>

<option value="HD-Cam">HD-Cam</option>
<option value="DV-Cam">DV-Cam</option>
<option value="Mini-DV">Mini-DV</option>
<option value="DVC-PRO HD">DVC-PRO HD</option>
</select>

3 comments:

NameFILIP said...

Very cool, thanks a lot! :)

NameFILIP said...

Very cool! Thank a lot :)

NameFILIP said...

oops, posted it twice :P... because it's double cool :)