How to use JSF h:messages better?

前端 未结 3 1337
执笔经年
执笔经年 2020-12-05 03:07

My Objective is to use h:messages to convey user - error and confirmation messages. The CSS styles to show these two different messages are different, In fact I

相关标签:
3条回答
  • 2020-12-05 03:37

    change <h:messages> to <h:message>

    <h:messages displays all messages for the current context, <h:message> displays a specific message.

    and I believe you want to change id to for to give it a target, but I could be wrong.

    0 讨论(0)
  • 2020-12-05 03:53

    The h:messages displays all messages, also the ones which are already displayed in a h:message in the page. You can however set it to only display messages with a null client ID using globalOnly="true".

    <h:messages globalOnly="true" />
    

    You can also give the message a different style depending on the FacesMessage.Severity:

    <h:messages infoClass="info" errorClass="error" />
    

    with for example this CSS which hides the INFO messages and makes ERROR messages red:

    .info {
         display: none;
    }
    .error {
         color: red;
    }
    

    You can use redisplay="false" to tell it to not display the already displayed messages via e.e. <h:message>.

    <h:messages redisplay="false" />
    

    You only need to make sure it's placed in the component tree after all those other message components. You can if necessary make use of CSS to reposition it somewhere in top.

    Just to be sure,

    facesContext.addMessage("clientId",  facesMessage);
    

    this will attach the given message to a <h:message for="clientId"> not to a <h:messages id="clientId"> as you seem to expect.

    0 讨论(0)
  • 2020-12-05 03:59

    We can have different types of h:messages for different severity levels. For example you might want to display all the error messages in a red box and rest with different styles like yellow for warn and green for info. You can also wrap around with different panels. You can do so by having different h:messages and apply the styles individually.

    <!--Displays only Error Messages-->
    <h:messages styleClass="mystyle" layout="list" id="msg1" infoStyle="display:none"       warnStyle="display:none"></h:messages>
    
    <!--Displays only Warning Messages-->
    <h:messages styleClass="messages" layout="list" id="msg2" errorStyle="display:none"     infoStyle="display:none"></h:messages>
    
    <!--Displays only Info Messages-->
    <h:messages styleClass="messages" layout="list" id="msg2" errorStyle="display:none"     warnStyle="display:none"></h:messages>
    
    0 讨论(0)
提交回复
热议问题