JSF Showcase

f:selectItems

This f:selectItems Facelet lets developers to add a Collection, Map or an array of SelectItem to have more programmatic control over the generated items.

General Usage

This component can be added together with other f:selectItem.

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:selectOneMenu value="#{inputTextModelBean.text}">
			<f:selectItems value="#{selectOneBackingBean.selectItems}"/>
			<f:selectItem itemLabel="#{i18n['item-4-label']}" itemValue="#{i18n['item-4-value']}"/>					
		</h:selectOneMenu>		
		<h:commandButton value="#{i18n['submit']}">
			<f:ajax execute="@form" render="@form"/>
		</h:commandButton>											
		<h:outputText value="#{inputTextModelBean.text}"/>
	</h:form>

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

	private Date date = new GregorianCalendar().getTime();
	private String text;

	public Date getDate() {
		return date;
	}

	public String getText() {
		return text;
	}

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

	public void setText(String text) {
		this.text = text;
	}
}
@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 4.0.0 + Showcase Common 3.0.0 + Liferay Faces Util 3.0.1-SNAPSHOT + Mojarra 2.2.13