0

I have some business logic code that renders some facesMessages on the facelet based on its output, so i made a method in the facelet managed bean like this method:

public void renderFacesMessages(String summary, String detail) {
    FacesMessage message = new FacesMessage(summary, detail);
    FacesContext.getCurrentInstance().addMessage(null, message);

}

and the business logic class will pass arguments to this method according to the message that's needed, the question is what is the right approach for business logic to call this method on the managed bean?

Eslam Hamdy
  • 7,126
  • 27
  • 105
  • 165

3 Answers3

1

It is all about the Layering Concept...

I presume you have a ManagedBean which has a method that will delegate the business logic to a seperate Business Class/Module. If this is the case,I would tell you ,NEVER have any Faces Methods on the Business Side...

Instead have the Business Results wrapped in a Class and return back to Managed Bean.This Result Class will encompass the Results,Meta information about the Result like,Errors,Exceptions.So now your managed Bean can use the renderFacesMessage method

Even if you had not followed the above presumption:My Suggestion Never have JSF Faces Logic inside Business Components.It will be a bad Idea.

1

Don't let the business logic call a JSF backing bean.

Instead let the backing bean call the business logic (eg an EJB bean or a transactional CDI bean in Java EE 7), then depending on the result of this call (exception, return value, etc) generate Faces messages and/or redirect to a new page etc.

Mike Braun
  • 3,729
  • 17
  • 15
0

As the businesslogic will be stateless (I guess/hope so), I would say that you should let the managed bean which called your businesslogic handle the error message display, by catching an exception e.g.

On the other hand, you can pass the managed bean to the businesslogic (or better, just an interface of the managed bean), so the businesslogic can callback the managed bean. But i would prefer the first approach.

Manuel
  • 3,828
  • 6
  • 33
  • 48