h:selectManyCheckbox

HtmlSelectManyCheckbox is a UISelectMany component that renders checkboxes and enables the user to select multiple values.

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.


Source Code

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

	<!-- Example 1: ValueChangeListener execution when immediate is true -->
	<h:form>
		<h:outputLabel value="#{i18n['favorite-liferay-benefit']}" />
		<h:messages globalOnly="true" styleClass="feedback" />
		<h:selectManyCheckbox id="selectManyCheckbox" immediate="true" layout="pageDirection" selectedClass="inline-label"
			unselectedClass="inline-label" value="#{selectManyModelBean.favoriteIds}"
			valueChangeListener="#{selectManyBackingBean.valueChangeListener}">
			<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:selectManyCheckbox>
		<hr />
		<h:commandButton 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>

	<!-- Example 2: ValueChangeListener execution when immediate is false (the default) -->
	<h:form>
		<h:outputLabel value="#{i18n['favorite-liferay-benefit']}" />
		<h:messages globalOnly="true" styleClass="feedback" />
		<h:selectManyCheckbox id="selectManyCheckbox" layout="pageDirection" selectedClass="inline-label"
			unselectedClass="inline-label" value="#{selectManyModelBean.favoriteIds}"
			valueChangeListener="#{selectManyBackingBean.valueChangeListener}">
			<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:selectManyCheckbox>
		<hr />
		<h:commandButton 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 5.0.0 + Showcase Common 3.1.1 + Liferay Faces Util 3.4.1 + Mojarra 2.2.20