SEAM and SelectOneMenu example

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;
}
}

Published by anthonykuong

Anthony is a versatile Software professional with around 10 years of experience. He is a Full Stack developer experienced with clients in the Financial, Health and Supply Chain industries. He is experienced with MVC frameworks ( Spring Boot) , SPA frameworks ( Angular , VueJS), and also supports automated build deployments and packaging for development, qa, and production servers.. He has delivered rich user experience using Modern web technologies and techniques such are HTML5, CSS3, ECMAScript 6 (ES6)/ ECMAScript 2015, CSS pre-processors (SASS, Less), JavaScript build tools (Grunt, Gulp) , various UI Frameworks including AngularJS , Knockout JS , and CSS Frameworks including Bootstrap, and Foundation. He is adaptable to new technologies and frameworks. He is a rigorous, quality-conscious contributor with solid analytical skills. I can also be found on youtube - Youtube Channel: https://www.youtube.com/user/akuong/

Leave a comment