Benchik Benchik - 8 months ago 27
Java Question

How to add a message to a specific component from java

I have an h:inputText and an h:message connected to it:

<h:inputText id="myText" value="#{myController.myText}" />
<h:message for="myText" .../>

I want to send a message to it from java, in a manner like:

FacesContext.getCurrentInstance().addMessage(arg0, arg1);

which is sent to h:messages, but to a specific id in a specific form.
How can I do this? (Without implementing validation bean or validation method - meaning without throwing validation exception).


You need to provide the so called client id, which you'll find on UIComponent.

The following is a quick example of how to use this.

Consider the following bean:

public class ComponentMsgBean {

    private UIComponent component;

    public UIComponent getComponent() {
        return component;

    public void setComponent(UIComponent component) {
        this.component = component;

    public String doAction() {

        FacesContext context = FacesContext.getCurrentInstance();

        context.addMessage(component.getClientId(), new FacesMessage("Test msg"));

        return "";


being used on the following Facelet:

<html xmlns=""


            <h:outputText id="test" value="test component" binding="#{componentMsgBean.component}"/>
            <h:message for="test"/>

            <h:commandButton value="click me" action="#{componentMsgBean.doAction}" />


This will add a Faces message with content "Test msg" for the outputText component used in the example.