JSF Showcase

h:selectManyListbox

HtmlSelectManyListbox is a UISelectMany component that renders a select element and enables the user to select multiple values.

General Usage

The value attribute can be bound to a model bean property via EL, and the size can be specified.

Source Code

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

	<h:form>
		<h:selectManyListbox id="selectManyListbox"
			required="#{showcaseModelBean.selectedComponent.required}"
			value="#{selectManyModelBean.favoriteIds}">
			<f:selectItem itemValue="#{null}" itemLabel="-- Select --" noSelectionOption="true" />
			<f:selectItem itemLabel="Compatible" itemValue="1" />
			<f:selectItem itemLabel="Enterprise Ready" itemValue="2" />
			<f:selectItem itemLabel="Powerful Integration" itemValue="3" />
			<f:selectItem itemLabel="Lightweight" itemValue="4" />
			<f:selectItem itemLabel="Open Source" itemValue="5" />
		</h:selectManyListbox>
		<h:message for="selectManyListbox" />
		<hr />
		<h:commandButton action="#{selectManyBackingBean.submit}" value="#{i18n['submit']}">
			<f:ajax execute="@form" render="@form" />
		</h:commandButton>
		<h:panelGroup id="modelValue">
			<ui:repeat value="#{selectManyModelBean.favoriteIds}" var="favoriteId">
				<h:outputText value="#{favoriteId}" /><br />
			</ui:repeat>
		</h:panelGroup>
	</h:form>

</ui:composition>
@ManagedBean
@RequestScoped
public class SelectManyModelBean {

	private List<Long> favoriteIds;
	private List<Long> benefitIds = Arrays.asList(2L, 4L);
	private List<Date> dates;
	private String phase;

	@ManagedProperty(name = "liferayBenefitService", value = "#{liferayBenefitService}")
	private LiferayBenefitService liferayBenefitService;

	public List<Long> getBenefitIds() {
		return benefitIds;
	}

	public List<Date> getDates() {
		return dates;
	}

	public List<Long> getFavoriteIds() {
		return favoriteIds;
	}

	public List<LiferayBenefit> getLiferayBenefits() {
		return liferayBenefitService.getLiferayBenefits();
	}

	public String getPhase() {
		return phase;
	}

	public void setBenefitIds(List<Long> benefitIds) {
		this.benefitIds = benefitIds;
	}

	public void setDates(List<Date> dates) {
		this.dates = dates;
	}

	public void setFavoriteIds(List<Long> favoriteIds) {
		this.favoriteIds = favoriteIds;
	}

	public void setLiferayBenefitService(LiferayBenefitService liferayBenefitService) {
		this.liferayBenefitService = liferayBenefitService;
	}

	public void setPhase(String phase) {
		this.phase = phase;
	}
}
@ManagedBean
@RequestScoped
public class SelectManyBackingBean {

	private static final Logger logger = LoggerFactory.getLogger(SelectManyBackingBean.class);

	@ManagedProperty(name = "selectManyModelBean", value = "#{selectManyModelBean}")
	private SelectManyModelBean selectManyModelBean;

	public void setSelectManyModelBean(SelectManyModelBean selectManyModelBean) {
		this.selectManyModelBean = selectManyModelBean;
	}

	public void submit() {
		PhaseId phaseId = FacesContext.getCurrentInstance().getCurrentPhaseId();
		logger.info("submit: phaseId=[{0}] favoriteId=[{1}]", phaseId.toString(), selectManyModelBean.getFavoriteIds());
	}

	public void submitAnswer() {
		List<Date> selectedDates = selectManyModelBean.getDates();
		TimeZone gmtTimeZone = TimeZone.getTimeZone("GMT");

		boolean correct = (selectedDates.size() > 0);

		for (Date selectedDate : selectedDates) {
			Calendar calendar = new GregorianCalendar(gmtTimeZone);
			calendar.setTime(selectedDate);

			int selectedYear = calendar.get(Calendar.YEAR);

			if ((selectedYear <= 1700) || (selectedYear > 1800)) {
				correct = false;

				break;
			}
		}

		FacesContext facesContext = FacesContext.getCurrentInstance();
		FacesMessage facesMessage;

		if (correct) {
			facesMessage = new FacesMessage("Correct!");
			facesMessage.setSeverity(FacesMessage.SEVERITY_INFO);
		}
		else {
			facesMessage = new FacesMessage("Incorrect!");
			facesMessage.setSeverity(FacesMessage.SEVERITY_ERROR);
		}

		facesContext.addMessage(null, facesMessage);
	}

	public void valueChangeListener(ValueChangeEvent valueChangeEvent) {

		FacesContext facesContext = FacesContext.getCurrentInstance();
		PhaseId phaseId = facesContext.getCurrentPhaseId();
		logger.debug("valueChangeListener: phaseId=[{0}]", phaseId.toString());

		String phaseName = phaseId.toString();
		FacesMessage facesMessage = new FacesMessage("The valueChangeListener method was called during the " +
				phaseName + " phase of the JSF lifecycle.");
		facesContext.addMessage(null, facesMessage);
	}
}
Liferay Faces Bridge Implementation 4.0.0 + Showcase Common 3.0.0 + Liferay Faces Util 3.0.1-SNAPSHOT + Mojarra 2.2.13