Several messages programmatically in JSF

I've got two places to put messages in my website. Messages related with an aside div and messages related with the main container.

I'm using this two h:messages simultaneusly within the same page (it's forms have prependId=false and no id so messages ids are not appended with others):

<h:messages id="container-messages" infoClass="info-message" warnClass="warn-message" errorClass="error-message" globalOnly="false" />

<h:messages id="aside-messages" infoClass="info-message" warnClass="warn-message" errorClass="error-message" globalOnly="false" />

Then inside my Java code I'm using this to print some messages:

facesContext.addMessage("container-messages", new FacesMessage(FacesMessage.SEVERITY_INFO, "one message", null));

The first problem is that this works, but it prints the message in both and not only in the container-messages.

Later I replaced with :

<h:message id="container-messages" infoClass="info-message" warnClass="warn-message" errorClass="error-message"/>

<h:message id="aside-messages" infoClass="info-message" warnClass="warn-message" errorClass="error-message" />

and tried also:

<h:message for="container-messages" infoClass="info-message" warnClass="warn-message" errorClass="error-message"/>

<h:message for="aside-messages" infoClass="info-message" warnClass="warn-message" errorClass="error-message" />

but in both cases it doesn't show anything.

Any ideas?


The h:messages is for so-called global messages which are unrelated to a specific component and therefore the for attribute is not allowed here.

The h:message is for messages related to a specific component, therefore the for attribute is required, this means you need a component to bind to.

To reach your goal you can use two h:message elements bound to some component (eg an empty h:grapicImage ) in the following way:

    <h:graphicImage id="someID" />
    <h:message for="someID" id="container-messages" infoClass="info-message" 
         warnClass="warn-message" errorClass="error-message"/>
    <div>
         stuff
    </div>
    <h:message for="someID" id="aside-messages" infoClass="info-message" 
         warnClass="warn-message" errorClass="error-message" />

Adding the messages in the way you already described:

    facesContext.addMessage("container-messages", new 
         FacesMessage(FacesMessage.SEVERITY_INFO, "message1", null));
    facesContext.addMessage("aside-messages", new 
         FacesMessage(FacesMessage.SEVERITY_INFO, "message2", null));

See also:

  • What does h:messages do in JSF?
  • jsf h:messages/ h:message for specific clientId
  • 链接地址: http://www.djcxy.com/p/71660.html

    上一篇: 了解PrimeFaces进程/更新和JSF f:ajax执行/渲染属性

    下一篇: 在JSF中以编程方式发送几条消息