h:selectManyMenu

HtmlSelectManyMenu is a UISelectMany component that renders a select element and enables the user to select multiple values. It has the same basic features of h:selectManyListbox except that there is no size attribute because JSF "select*menu" components always render size=1.

Immediate Usage

When the immediate attribute is true, the submitted value is converted and validated during APPLY_REQUEST_VALUES phase of the JSF lifecycle, rather than the normal PROCESS_VALIDATIONS phase.
Note: Chromium Issue#4579 may cause this component to display more than one menu item.


Source Code

  1. <ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://xmlns.jcp.org/jsf/html"
  2. xmlns:f="http://xmlns.jcp.org/jsf/core" xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
  3.  
  4. <!-- Example 1: ValueChangeListener execution when immediate is true -->
  5. <h:form>
  6. <h:outputLabel value="#{i18n['favorite-liferay-benefit']}" />
  7. <h:messages globalOnly="true" styleClass="feedback" />
  8. <h:selectManyMenu id="selectManyMenu" immediate="true" value="#{selectManyModelBean.favoriteIds}"
  9. valueChangeListener="#{selectManyBackingBean.valueChangeListener}">
  10. <f:selectItem itemLabel="Compatible" itemValue="1" />
  11. <f:selectItem itemLabel="Enterprise Ready" itemValue="2" />
  12. <f:selectItem itemLabel="Powerful Integration" itemValue="3" />
  13. <f:selectItem itemLabel="Lightweight" itemValue="4" />
  14. <f:selectItem itemLabel="Open Source" itemValue="5" />
  15. </h:selectManyMenu>
  16. <h:outputText escape="false" value="#{i18n['note-chromium-issue-4579']}" />
  17. <hr />
  18. <h:commandButton value="#{i18n['submit']}">
  19. <f:ajax execute="@form" render="@form" />
  20. </h:commandButton>
  21. <h:panelGroup id="modelValue">
  22. <ui:repeat value="#{selectManyModelBean.favoriteIds}" var="favoriteId">
  23. <h:outputText value="#{favoriteId}" /><br />
  24. </ui:repeat>
  25. </h:panelGroup>
  26. </h:form>
  27.  
  28. <!-- Example 2: ValueChangeListener execution when immediate is false (the default) -->
  29. <h:form>
  30. <h:outputLabel value="#{i18n['favorite-liferay-benefit']}" />
  31. <h:messages globalOnly="true" styleClass="feedback" />
  32. <h:selectManyMenu id="selectManyMenu" value="#{selectManyModelBean.favoriteIds}"
  33. valueChangeListener="#{selectManyBackingBean.valueChangeListener}">
  34. <f:selectItem itemLabel="Compatible" itemValue="1" />
  35. <f:selectItem itemLabel="Enterprise Ready" itemValue="2" />
  36. <f:selectItem itemLabel="Powerful Integration" itemValue="3" />
  37. <f:selectItem itemLabel="Lightweight" itemValue="4" />
  38. <f:selectItem itemLabel="Open Source" itemValue="5" />
  39. </h:selectManyMenu>
  40. <hr />
  41. <h:commandButton value="#{i18n['submit']}">
  42. <f:ajax execute="@form" render="@form" />
  43. </h:commandButton>
  44. <h:panelGroup id="modelValue">
  45. <ui:repeat value="#{selectManyModelBean.favoriteIds}" var="favoriteId">
  46. <h:outputText value="#{favoriteId}" /><br />
  47. </ui:repeat>
  48. </h:panelGroup>
  49. </h:form>
  50.  
  51. </ui:composition>
  1. @ManagedBean
  2. @RequestScoped
  3. public class SelectManyModelBean {
  4.  
  5. private List<Long> favoriteIds;
  6. private List<Long> benefitIds = Arrays.asList(2L, 4L);
  7. private List<Date> dates;
  8. private String phase;
  9.  
  10. @ManagedProperty(name = "liferayBenefitService", value = "#{liferayBenefitService}")
  11. private LiferayBenefitService liferayBenefitService;
  12.  
  13. public List<Long> getBenefitIds() {
  14. return benefitIds;
  15. }
  16.  
  17. public List<Date> getDates() {
  18. return dates;
  19. }
  20.  
  21. public List<Long> getFavoriteIds() {
  22. return favoriteIds;
  23. }
  24.  
  25. public List<LiferayBenefit> getLiferayBenefits() {
  26. return liferayBenefitService.getLiferayBenefits();
  27. }
  28.  
  29. public String getPhase() {
  30. return phase;
  31. }
  32.  
  33. public void setBenefitIds(List<Long> benefitIds) {
  34. this.benefitIds = benefitIds;
  35. }
  36.  
  37. public void setDates(List<Date> dates) {
  38. this.dates = dates;
  39. }
  40.  
  41. public void setFavoriteIds(List<Long> favoriteIds) {
  42. this.favoriteIds = favoriteIds;
  43. }
  44.  
  45. public void setLiferayBenefitService(LiferayBenefitService liferayBenefitService) {
  46. this.liferayBenefitService = liferayBenefitService;
  47. }
  48.  
  49. public void setPhase(String phase) {
  50. this.phase = phase;
  51. }
  52. }
  1. @ManagedBean
  2. @RequestScoped
  3. public class SelectManyBackingBean {
  4.  
  5. private static final Logger logger = LoggerFactory.getLogger(SelectManyBackingBean.class);
  6.  
  7. @ManagedProperty(name = "selectManyModelBean", value = "#{selectManyModelBean}")
  8. private SelectManyModelBean selectManyModelBean;
  9.  
  10. public void setSelectManyModelBean(SelectManyModelBean selectManyModelBean) {
  11. this.selectManyModelBean = selectManyModelBean;
  12. }
  13.  
  14. public void submit() {
  15. PhaseId phaseId = FacesContext.getCurrentInstance().getCurrentPhaseId();
  16. logger.info("submit: phaseId=[{0}] favoriteId=[{1}]", phaseId.toString(), selectManyModelBean.getFavoriteIds());
  17. }
  18.  
  19. public void submitAnswer() {
  20. List<Date> selectedDates = selectManyModelBean.getDates();
  21. TimeZone gmtTimeZone = TimeZone.getTimeZone("GMT");
  22.  
  23. boolean correct = (selectedDates.size() > 0);
  24.  
  25. for (Date selectedDate : selectedDates) {
  26. Calendar calendar = new GregorianCalendar(gmtTimeZone);
  27. calendar.setTime(selectedDate);
  28.  
  29. int selectedYear = calendar.get(Calendar.YEAR);
  30.  
  31. if ((selectedYear <= 1700) || (selectedYear > 1800)) {
  32. correct = false;
  33.  
  34. break;
  35. }
  36. }
  37.  
  38. FacesContext facesContext = FacesContext.getCurrentInstance();
  39. FacesMessage facesMessage;
  40.  
  41. if (correct) {
  42. facesMessage = new FacesMessage("Correct!");
  43. facesMessage.setSeverity(FacesMessage.SEVERITY_INFO);
  44. }
  45. else {
  46. facesMessage = new FacesMessage("Incorrect!");
  47. facesMessage.setSeverity(FacesMessage.SEVERITY_ERROR);
  48. }
  49.  
  50. facesContext.addMessage(null, facesMessage);
  51. }
  52.  
  53. public void valueChangeListener(ValueChangeEvent valueChangeEvent) {
  54.  
  55. FacesContext facesContext = FacesContext.getCurrentInstance();
  56. PhaseId phaseId = facesContext.getCurrentPhaseId();
  57. logger.debug("valueChangeListener: phaseId=[{0}]", phaseId.toString());
  58.  
  59. String phaseName = phaseId.toString();
  60. FacesMessage facesMessage = new FacesMessage("The valueChangeListener method was called during the " +
  61. phaseName + " phase of the JSF lifecycle.");
  62. facesContext.addMessage(null, facesMessage);
  63. }
  64. }
Liferay Faces Bridge Implementation 5.0.0 + Showcase Common 3.1.1 + Liferay Faces Util 3.4.1 + Mojarra 2.2.20