Creating FacesMessage in an action method outside of the JSF transform / validation mechanism?

I am currently learning jsf 2.0 from the main jsf 2.0 book + glassfish + cdi.

I would like to ask a question about handling validations that are not defined on jsf pages or managed / named beans with bean -validation-framework. I got these tiers in my head:

  • 1) ui tier / jsf pages
  • 1.5) jsf managed / named beans (I use 1.5 because I think it is still tightly coupled to the jsf level, e.g. beans support)
  • 2) the level of business logic (which is pure from jsf stuffs / import, does only pure business logic)
  • 3) save level

I assume that level 1.5 (jsf bean) initializes and calls level 2 (business logic objects), providing arguments when invoking business methods, getting the result, filling the result in jsf bean properties so that ui can render correctly.

Interestingly, level 2 (business logic objects) could perform checks on the provided arguments or check data, etc. and could throw exceptions or error objects.

I think I can handle exceptions and get error objects at level 1.5 (jsf managed beans), but how should I display the error on the displayed pages? It seems I can’t find it from the book by reading the book, but I hope you have a way to create a global error message and somehow enter it somewhere so that it appears in the tag?

Thank!

+4
1

FacesContext#addMessage(), FacesMessage .

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

null, . <h:messages />

<h:messages globalOnly="true" />

globalOnly="true" null.

.

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

<h:form id="formid">
    <h:inputText id="inputid" />
    <h:message for="inputid" />
+25

All Articles