h:messages

HtmlMessages is a UIMessages component that renders the text of one or more FacesMessage instances inside a <ul> element or <table> element for an associated component, according to the layout attribute.

General Usage

The component is typically placed inside an h:form in order to render messages within the context of a form.



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">

	<!-- Example 1: Render all messages in a <ul> (the default) -->
	<h:form>
		<h:messages />
		<h:outputLabel for="inputText" value="#{i18n['text-field']}" />
		<h:inputText id="inputText" label="#{i18n['text-field']}" required="true"
			valueChangeListener="#{messageBackingBean.valueChangeListener}" />
		<br />
		<h:commandButton action="#{messageBackingBean.submit}" value="#{i18n['submit-and-re-render-form']}">
			<f:ajax execute="@form" render="@form" />
		</h:commandButton>
	</h:form>

	<!-- Example 2: Render all messages in a <table> -->
	<h:form>
		<h:messages layout="table" />
		<h:outputLabel for="inputText" value="#{i18n['text-field']}" />
		<h:inputText id="inputText" label="#{i18n['text-field']}" required="true"
			valueChangeListener="#{messageBackingBean.valueChangeListener}" />
		<br />
		<h:commandButton action="#{messageBackingBean.submit}" value="#{i18n['submit-and-re-render-form']}">
			<f:ajax execute="@form" render="@form" />
		</h:commandButton>
	</h:form>

	<!-- Example 3: Render only global messages in a <ul> -->
	<h:form>
		<h:messages globalOnly="true" layout="table" />
		<h:outputLabel for="inputText" value="#{i18n['text-field']}" />
		<h:inputText id="inputText" label="#{i18n['text-field']}" required="true"
			valueChangeListener="#{messageBackingBean.valueChangeListener}" />
		<br />
		<h:commandButton action="#{messageBackingBean.submit}" value="#{i18n['submit-and-re-render-form']}">
			<f:ajax execute="@form" render="@form" />
		</h:commandButton>
	</h:form>

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

	public void submit() {

		FacesMessage globalFacesMessage = new FacesMessage("Your request processed successfully.");
		FacesContext facesContext = FacesContext.getCurrentInstance();
		facesContext.addMessage(null, globalFacesMessage);
	}

	public void valueChangeListener(ValueChangeEvent valueChangeEvent) {

		Object newValue = valueChangeEvent.getNewValue();
		int totalChars = 0;

		if (newValue != null) {
			totalChars = newValue.toString().length();
		}

		FacesMessage globalFacesMessage = new FacesMessage("You typed " + totalChars + " characters.");
		FacesContext facesContext = FacesContext.getCurrentInstance();
		String componentClientId = valueChangeEvent.getComponent().getClientId();
		facesContext.addMessage(componentClientId, globalFacesMessage);
	}
}
Liferay Faces Bridge Implementation 5.0.0 + Showcase Common 3.1.1 + Liferay Faces Util 3.4.1 + Mojarra 2.2.20