Search code examples
jsfmodel-view-controllerfront-controller

How to tell which class handles a request in Java Server Faces?


I'm taking over a project which is based on Java Server Faces (project created circa 2009, so JSF 1.0?). I haven't worked out how JSF handles the front controller logic, i.e. how the Faces servlet determines which Java class to forward a GET or POST to. For example, Spring MVC has the "@Request" annotation. How does JSF do it?

This is a very useful link, but didn't answer my question specifically. I wanted know how JSF tied a GET or POST to Java class. It turned out that it's the "action" item in the ice:commandButton, e.g.

        <ice:form id="footForm">
            <ice:commandButton id="cancelButton" value="Cancel" action="#{ProductMB.cancel}"></ice:commandButton>
            <ice:commandButton id="saveButton" value="Save" action="#{ProductMB.save}" disabled="#{ProductMB.notAllRequiredFieldsEntered}"></ice:commandButton>
        </ice:form>

Solution

  • The out of the box model for JSF is a bit different than what you are used to with spring mvc. The request handlers are usually mapped to the jsp files under the /webapp directory, so the /webapp/home.xhtml file is served under /yourwebapp/home.jsf

    The JSF pages typically pull their dependencies from a managed bean context using the expression language. These beans are either declared in a faces-config.xml application configuration file (which looks a lot like a spring config file). If that's the case, you'll be able to see where all the managed beans live. Otherwise, they'll be declared via the @ManagedBean annotation on the managed bean classes themselves.

    I think there are many other flavors of JSF, but this tutorial should give you start on the way the vanilla version works:

    http://www.tutorialspoint.com/jsf/jsf_first_application.htm