h:message

HtmlMessage is a UIMessage component that renders a <span> element containing the text of a single FacesMessage for an associated component.

General Usage

The component can be associated with an input component like h:inputText via the for attribute. If it is a child of h:field then it will be automatically styled when validation fails.

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:inputText id="inputText" required="true" styleClass="field"
			valueChangeListener="#{messageBackingBean.valueChangeListener}" />
		<h:message for="inputText" />
		<h:commandButton value="#{i18n['submit-and-re-render-field']}">
			<f:ajax execute="@form" render="field" />
		</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