Creating FacesMessage in action method outside JSF conversion/validation mechanism?

前端 未结 1 457
天命终不由人
天命终不由人 2020-11-27 07:01

I\'m currently learning about jsf 2.0 from core jsf 2.0 book + glassfish + cdi.

I would like to ask a question about handling validations that are not defined in the

相关标签:
1条回答
  • 2020-11-27 07:33

    You can use FacesContext#addMessage() to add a FacesMessage to the context programmatically.

    FacesContext facesContext = FacesContext.getCurrentInstance();
    FacesMessage facesMessage = new FacesMessage("This is a message");
    facesContext.addMessage(null, facesMessage);
    

    When you set the client ID argument with null, it will become a global message. You can display and filter them using <h:messages />

    <h:messages globalOnly="true" />
    

    The globalOnly="true" will display only messages with a null client ID.

    You can however also specify a specific client ID.

    facesContext.addMessage("formid:inputid", facesMessage);
    

    This one will then end up in

    <h:form id="formid">
        <h:inputText id="inputid" />
        <h:message for="inputid" />
    
    0 讨论(0)
提交回复
热议问题