JSF Showcase

ui:repeat

ui:repeat iterates through a List, array, ResultSet or a single Object, encoding the children nested elements for each iteration. For these iterations you can use var attribute as the name of an scoped object that will be available for use.

General Usage

Use size if you want to delimit the number of iterations (should be less or equal from the total value elements), or step to only process those elements after the latest processed one. There is an interesting request variable that can be set in varStatus with some internal data about each iteration.
  • Index: 0, China
  • Index: 1, Switzerland
  • Index: 2, United States
  • Index: 3, United Kingdom
  • Index: 4, Vietnam

Source Code

<ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://xmlns.jcp.org/jsf/html"
	xmlns:ui="http://xmlns.jcp.org/jsf/facelets">

	<ul>
	<ui:repeat value="#{countryService.allCountries}" var="country" varStatus="metadata">
		<li style="background-color:#{ (metadata.odd ? 'grey' : 'white')}">
			<h:outputText value="#{i18n['index']}: #{metadata.index}, #{country.countryName}"/>
		</li>
	</ui:repeat>
	</ul>

</ui:composition>
@ApplicationScoped
@ManagedBean(name = "countryService")
public class CountryServiceMockImpl implements CountryService, Serializable {

	// serialVersionUID
	private static final long serialVersionUID = 4289537697479875863L;

	// Private Data Members
	private List<Country> countryList;
	private Map<Long, Country> countryMap;

	@Override
	public List<Country> getAllCountries() {
		return countryList;
	}

	@Override
	public Country getCountryByCode(String countryCode) {

		Country country = null;

		for (Country curCountry : countryList) {

			if (curCountry.getCountryCode().equals(countryCode)) {
				country = curCountry;

				break;
			}
		}

		return country;
	}

	@Override
	public Map<Long, Country> getCountryMap() {
		return countryMap;
	}

	@PostConstruct
	public void postConstruct() {
		countryMap = new HashMap<Long, Country>();

		Country country = new Country(1, "CN", "China");
		countryMap.put(country.getCountryId(), country);
		country = new Country(2, "CH", "Switzerland");
		countryMap.put(country.getCountryId(), country);
		country = new Country(3, "US", "United States");
		countryMap.put(country.getCountryId(), country);
		country = new Country(4, "UK", "United Kingdom");
		countryMap.put(country.getCountryId(), country);
		country = new Country(5, "VN", "Vietnam");
		countryMap.put(country.getCountryId(), country);
		countryList = new ArrayList<Country>(countryMap.values());
	}
}
public class Country implements Serializable {

	// serialVersionUID
	private static final long serialVersionUID = 9135922470541735463L;

	// Private Data Members
	private long countryId;
	private String countryCode;
	private String countryName;

	public Country(long countryId, String countryCode, String countryName) {
		this.countryId = countryId;
		this.countryCode = countryCode;
		this.countryName = countryName;
	}

	public String getCountryCode() {
		return countryCode;
	}

	public long getCountryId() {
		return countryId;
	}

	public String getCountryName() {
		return countryName;
	}

	public void setCountryCode(String countryCode) {
		this.countryCode = countryCode;
	}

	public void setCountryId(long countryId) {
		this.countryId = countryId;
	}

	public void setCountryName(String countryName) {
		this.countryName = countryName;
	}
}
Liferay Faces Bridge Implementation 4.0.0 + Showcase Common 3.0.0 + Liferay Faces Util 3.0.1-SNAPSHOT + Mojarra 2.2.13