h:selectOneListbox

HtmlSelectOneListbox is a UISelectOne component that renders a select element (multi-item list) and enables the user to select one value.

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:selectOneListbox id="selectOneListbox"
			required="#{showcaseModelBean.selectedComponent.required}" size="4"
			value="#{selectOneModelBean.favoriteId}">
			<f:selectItem 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:selectOneListbox>
		<h:message for="selectOneListbox" />
		<hr />
		<h:commandButton action="#{selectOneBackingBean.submit}" value="#{i18n['submit']}">
			<f:ajax execute="@form" render="@form" />
		</h:commandButton>
		<h:outputText id="modelValue" value="#{selectOneModelBean.favoriteId}" />
	</h:form>

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

	private Long favoriteId;
	private Long benefitId = 3L;
	private Date date;
	private String phase;

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

	public Long getBenefitId() {
		return benefitId;
	}

	public Date getDate() {
		return date;
	}

	public Long getFavoriteId() {
		return favoriteId;
	}

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

	public String getPhase() {
		return phase;
	}

	public void setBenefitId(Long benefitId) {
		this.benefitId = benefitId;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public void setFavoriteId(Long favoriteId) {
		this.favoriteId = favoriteId;
	}

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

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

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

	@ManagedProperty(name = "selectOneModelBean", value = "#{selectOneModelBean}")
	private SelectOneModelBean selectOneModelBean;

	public SelectItem[] getSelectItems() {
		SelectItem[] selectItems = new SelectItem[3];

		for (int i = 0; i < 3; i++) {
			SelectItem item = new SelectItem();
			item.setLabel("Item " + (i + 1));
			item.setValue(i + 1);
			selectItems[i] = item;
		}

		return selectItems;
	}

	public void setSelectOneModelBean(SelectOneModelBean selectOneModelBean) {
		this.selectOneModelBean = selectOneModelBean;
	}

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

	public void submitAnswer() {

		Date selectedDate = selectOneModelBean.getDate();

		TimeZone gmtTimeZone = TimeZone.getTimeZone("GMT");
		Calendar calendar = new GregorianCalendar(gmtTimeZone);

		if (selectedDate != null) {
			calendar.setTime(selectedDate);
		}

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

		if ((selectedDate != null) && (calendar.get(Calendar.MONTH) == 6) && (calendar.get(Calendar.DATE) == 4) &&
				(calendar.get(Calendar.YEAR) == 1776)) {
			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