2
votes

I'm developping a web app, with jsf2,spring and hibernate.

I have an "application scoped" managed bean (called "utilsJSF") with JSF util methods, such as:

  • Reading properties files for getting a message from its key (using getBundle).
  • Adding a string as a facesmessage to be shown later on a view.
  • etc

I inject this bean in my "Base Controller" (as a "managed property"), so that all my controllers extend this one, and can access those utilities. Here I have no problem.

But, how can I use these utilities from other classes that are not "managed beans"?

I will explain myself:

I have an exception hierarchy, where each particular exception class has to access a resource bundle (property file), where the key is the name of the exception and the value is the message I'm gonna show to the user. The exception constructor gets the value from the file, stores it in a field of the exception, and then the controller shows the message to the user as a facesmessage.

I can show the message from the controller using the "utilsJSF" managed bean, because I injected it into the BaseController.

But I cannot inject "utilsJSF" into the exception class in order to use it (because the exception classes are not managed beans).

What's the best solution to solve this problem?

1

1 Answers

2
votes

You should refactor all those utility methods (the ones which are (or can be) public static), into a real utility class, not keep it in an application scoped bean.

public final class Faces {

    private Faces() {
        // Prevent construction.
    }

    public static void addGlobalInfoMessage(String summary) {
        FacesContext.getCurrentInstance().addMessage(null,
            new FacesMessage(FacesMessage.SEVERITY_INFO, summary, null));
    }

    // ...
}

This way you can use it everywhere. Create a similar one for getting bundle messages.