c:forEach

c:forEach have similar features and attributes than ui:repeat.

General Usage

  • INDEX: 0, China
  • INDEX: 1, Switzerland
  • INDEX: 2, United States
  • INDEX: 3, United Kingdom
  • INDEX: 4, Vietnam

Source Code

  1. <ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:c="http://xmlns.jcp.org/jsp/jstl/core"
  2. xmlns:h="http://xmlns.jcp.org/jsf/html" xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
  3.  
  4. <ul>
  5. <c:forEach items="#{countryService.allCountries}" var="country" varStatus="metadata">
  6. <li style="background-color:#{ (metadata.odd ? 'grey' : 'white')}">
  7. <h:outputText value="#{i18n['index']}: #{metadata.index}, #{country.countryName}"/>
  8. </li>
  9. </c:forEach>
  10. </ul>
  11.  
  12. </ui:composition>
  1. @ApplicationScoped
  2. @ManagedBean(name = "countryService")
  3. public class CountryServiceMockImpl implements CountryService, Serializable {
  4.  
  5. // serialVersionUID
  6. private static final long serialVersionUID = 4289537697479875863L;
  7.  
  8. // Private Data Members
  9. private List<Country> countryList;
  10. private Map<Long, Country> countryMap;
  11.  
  12. @Override
  13. public List<Country> getAllCountries() {
  14. return countryList;
  15. }
  16.  
  17. @Override
  18. public Country getCountryByCode(String countryCode) {
  19.  
  20. Country country = null;
  21.  
  22. for (Country curCountry : countryList) {
  23.  
  24. if (curCountry.getCountryCode().equals(countryCode)) {
  25. country = curCountry;
  26.  
  27. break;
  28. }
  29. }
  30.  
  31. return country;
  32. }
  33.  
  34. @Override
  35. public Map<Long, Country> getCountryMap() {
  36. return countryMap;
  37. }
  38.  
  39. @PostConstruct
  40. public void postConstruct() {
  41. countryMap = new HashMap<Long, Country>();
  42.  
  43. Country country = new Country(1, "CN", "China");
  44. countryMap.put(country.getCountryId(), country);
  45. country = new Country(2, "CH", "Switzerland");
  46. countryMap.put(country.getCountryId(), country);
  47. country = new Country(3, "US", "United States");
  48. countryMap.put(country.getCountryId(), country);
  49. country = new Country(4, "UK", "United Kingdom");
  50. countryMap.put(country.getCountryId(), country);
  51. country = new Country(5, "VN", "Vietnam");
  52. countryMap.put(country.getCountryId(), country);
  53. countryMap = Collections.unmodifiableMap(countryMap);
  54. countryList = Collections.unmodifiableList(new ArrayList<Country>(countryMap.values()));
  55. }
  56. }
  1. public class Country implements Serializable {
  2.  
  3. // serialVersionUID
  4. private static final long serialVersionUID = 9135922470541735463L;
  5.  
  6. // Private Data Members
  7. private long countryId;
  8. private String countryCode;
  9. private String countryName;
  10.  
  11. public Country(long countryId, String countryCode, String countryName) {
  12. this.countryId = countryId;
  13. this.countryCode = countryCode;
  14. this.countryName = countryName;
  15. }
  16.  
  17. public String getCountryCode() {
  18. return countryCode;
  19. }
  20.  
  21. public long getCountryId() {
  22. return countryId;
  23. }
  24.  
  25. public String getCountryName() {
  26. return countryName;
  27. }
  28.  
  29. public void setCountryCode(String countryCode) {
  30. this.countryCode = countryCode;
  31. }
  32.  
  33. public void setCountryId(long countryId) {
  34. this.countryId = countryId;
  35. }
  36.  
  37. public void setCountryName(String countryName) {
  38. this.countryName = countryName;
  39. }
  40. }
Liferay Faces Bridge Implementation 5.0.0 + Showcase Common 3.1.1 + Liferay Faces Util 3.4.1 + Mojarra 2.2.20