Example usage of SelectOneMenu
In this example i want to use selectOneMenu to select a country from list of countries and save the country code for the user in USER_ACCOUNT table.
<h:selectOneMenu id= "country" value= "#{userForm.countryCode}" require= "true" >
<s:selectItems value= "#{countries.list}" var= "country" label= "#{country.countryName}" />
<s:convertEntity/>
</h:selectOneMenu>
In the above code “userForm.countryCode” refers to model “UserForm” with property countryCode, The helper class to load countries list is as follows , the scope is application as the countries and their code doesn’t change.
@Name ( "countries" ) @Scope ( ScopeType . APPLICATION )
public class CountriesList {
@Outprivate List < country > list ;
@In ( create = true ) EntityManager entityManager ;
@Factory ( "countriesList" )
public List < country > getList () {
Query query = entityManager . createNamedQuery ( "Country.findAll" );
list = query . getResultList ();
return list ;
}
public void setCountriesList ( List < country > list ) {
this . list = list ;
}
}
Entity class Country is as follows,
@Entity@Name ( "country" ) @Table ( name = "COUNTRY" )
@NamedQueries ({ @NamedQuery ( name = "Country.findAll" , query = "SELECT c FROM Country c ORDER BY c.countryName" ), @NamedQuery ( name = "Country.findByCountryCode" , query = "SELECT c FROM Country c WHERE c.countryCode = :countryCode" ), @NamedQuery ( name = "Country.findByCountryName" , query = "SELECT c FROM Country c WHERE c.countryName = :countryName" )})
public class Country extends Model implements Serializable { private static final long serialVersionUID = 1L ;
@Id@Column ( name = "COUNTRY_CODE" , nullable = false )
private String countryCode ; @Column ( name = "COUNTRY_NAME" , nullable = false )
private String countryName ;
public Country () {}
public Country ( String countryCode ) {
this . countryCode = countryCode ;
}
public Country ( String countryCode , String countryName ) {
this . countryCode = countryCode ;
this . countryName = countryName ;
}
public String getCountryCode () {
return countryCode ;
}
public void setCountryCode ( String countryCode ) {
this . countryCode = countryCode ;
}
public String getCountryName () {
return countryName ;
} public void setCountryName ( String countryName ) {
this . countryName = countryName ;
}
}