In JSF, tag is used to render a dropdown box – HTML select element with “size=1” attribute. The following example
will show how to select a City from list of Cities and save the Postal Code for a given user using SEAM .
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:head />
<body>
<h:form id="userForm">
<h:selectOneMenu id="city" value="#{userForm.postalCode}" require="true">
<s:selectItems value="#{cities.list}" var="city" label="#{city.cityName}"/>
<s:convertEntity/>
</h:selectOneMenu>
</h:form>
</body>
</html>
In the above code “userForm.postalCode” refers to model “UserForm” with property postalCode,
The helper class to load cities list is as follows , the scope is application as the cities and their postal code doesn’t change.
@Name("cities")
@Scope(ScopeType.APPLICATION)
public class CitiesList {
@Out
private List<City> list;
@In(create=true)
EntityManager entityManager;
@Factory("citiesList")
public List<City> getList() {
Query query=entityManager.createNamedQuery("City.findAll");
list=query.getResultList();
return list;
}
public void setCitiesList(List<City> list) {
this.list = list;
}
}
</city></cityy></city>
Entity class City is as follows,
@Entity
@Name("City")
@Table(name = "CITY")
@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 City extends Model implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "POSTAL_CODE", nullable = false)
private String postalCode;
@Column(name = "CITY_NAME", nullable = false)
private String cityName;
public City() {
}
public City(String postalCode) {
this.postalCode = postalCode;
}
public City(String postalCode, String cityName) {
this.postalCode = postalCode;
this.cityName = cityName;
}
public String getPostalCode() {
return postalCode;
}
public void setPostalCode(String postalCode) {
this.postalCode = postalCode;
}
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
}