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 jsf pages or managed/named beans with bean-validation-framework. I got these tiers in my head :
I imagine tier 1.5(jsf bean) initializing and calling tier 2(business logic objects), supplying arguments when calling business methods, fetching result, populating the result into jsf bean properties, so that the ui could render correctly.
What im curious is the fact that the tier 2(business logic objects) could do validations on the supplied arguments, or validating data, etc, and could throw exceptions or error objects.
I think i could handle the exceptions and get the error objects in the tier 1.5(jsf managed beans), but how am i supposed to display the error in the rendered pages ? I cant seem to find it from the book im reading, but i'm hoping there's a way to create a global error message and somehow could inject it into somewhere so that it gets rendered by the tag ?
Thank you !
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" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With