Quantcast
Channel: Baeldung
Viewing all articles
Browse latest Browse all 4536

JavaServer Faces (JSF) with Spring

$
0
0

I just released the Starter Class of "Learn Spring Security":

>> CHECK OUT THE COURSE

1. Overview

In this article we will look at a recipe for accessing beans defined in Spring from within a JSF managed bean and a JSF page, for the purposes of delegating the execution of business logic to the Spring beans.

This article presumes the reader has a prior understanding of both JSF and Spring separately. The article is based on the Mojarra implementation of JSF.

2. In Spring

Let’s have the following bean defined in Spring. The UserManagementDAO bean adds a username to an in-memory store, and its defined by the following interface:

public interface UserManagementDAO {
    boolean createUser(String newUserData);
}

The implementation of the bean is configured using the following Java config:

public class SpringCoreConfig {
    @Bean
    public UserManagementDAO userManagementDAO() {
        return new UserManagementDAOImpl();
    }
}

Or using the following XML configuration:

<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
<bean class="com.baeldung.dao.UserManagementDAOImpl" id="userManagementDAO"/>

We define the bean in XML, and register CommonAnnotationBeanPostProcessor to ensure that the @PostConstruct annotation is picked up.

3. Configuration

The following sections explain the configuration items that enable the integration of the Spring and JSF contexts.

3.1. Java Configuration Without web.xml

By implementing the WebApplicationInitializer we are able to programatically configure the ServletContext. The following is the onStartup() implementation inside the MainWebAppInitializer class:

public void onStartup(ServletContext sc) throws ServletException {
    AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext();
    root.register(SpringCoreConfig.class);
    sc.addListener(new ContextLoaderListener(root));
}

The AnnotationConfigWebApplicationContext bootstraps the Spring’g context and adds the beans by registering the SpringCoreConfig class.

Similarly, in the Mojarra implementation there is a FacesInitializer class that configures the FacesServlet. To use this configuration it is enough to extend the FacesInitializer. The complete implementation of the MainWebAppInitializer, is now as follows:

public class MainWebAppInitializer extends FacesInitializer implements WebApplicationInitializer {
    public void onStartup(ServletContext sc) throws ServletException {
        AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext();
        root.register(SpringCoreConfig.class);
        sc.addListener(new ContextLoaderListener(root));
    }
}

3.2. With web.xml

We’ll start by configuring the ContextLoaderListener in web.xml file of the application:

<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>

This listener is responsible for starting up the Spring application context when the web application starts up. This listener will look for a spring configuration file named applicationContext.xml by default.

3.3. faces-config.xml

We now configure the SpringBeanFacesELResolver in the face-config.xml file:

<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>

An EL resolver is a pluggable component supported by the JSF framework, allowing us to customize the behavior of the JSF runtime when evaluating Expression Language (EL) expressions. This EL resolver will allow the JSF runtime access Spring components via EL expressions defined in JSF.

4. Accessing Spring Beans in JSF

At this point, our JSF web application is primed to access our Spring bean from either a JSF backing bean, or from a JSF page.

4.1. From a Backing Bean JSF 2.0

The Spring bean can now be accessed from a JSF backing bean. Depending on the version of JSF you’re running, there are two possible methods. With JSF 2.0, you use the @ManagedProperty annotation on the JSF managed bean.

@ManagedBean(name = "registration")
@RequestScoped
public class RegistrationBean implements Serializable {
    @ManagedProperty(value = "#{userManagementDAO}")
    transient private IUserManagementDAO theUserDao;

    private String userName;
    // getters and setters
}

Note that the getter and setter are mandatory when using the @ManagedProperty. 
Now – to assert the accessibility of a Spring bean from a managed bean, we will add the createNewUser() method:

public void createNewUser() {
    FacesContext context = FacesContext.getCurrentInstance();
    boolean operationStatus = userDao.createUser(userName);
    context.isValidationFailed();
    if (operationStatus) {
        operationMessage = "User " + userName + " created";
    }
}

The gist of the method is using the userDao Spring bean, and accessing its functionality.

4.2. From a Backing Bean in JSF 2.2

Another approach, valid only in JSF2.2 and above, is to use CDI’s @Inject annotation. This is applicable to JSF managed beans (with the @ManagedBean annotation), and CDI-managed beans (with the @Named annotation).

Indeed, with a CDI annotation, this is the only valid method of injecting the bean:

@Named( "registration")
@RequestScoped
public class RegistrationBean implements Serializable {
    @Inject
    UserManagementDAO theUserDao;
}

With this approach, the getter and setter are not necessary. Also note that the EL expression is absent.

4.3. From a JSF View

The createNewUser() method will be triggered from the following JSF page:

<h:form>
    <h:panelGrid id="theGrid" columns="3">
        <h:outputText value="Username"/>
        <h:inputText id="firstName" binding="#{userName}" required="true"
          requiredMessage="#{msg['message.valueRequired']}" value="#{registration.userName}"/>
        <h:message for="firstName" style="color:red;"/>
        <h:commandButton value="#{msg['label.saveButton']}" action="#{registration.createNewUser}"
          process="@this"/>
        <h:outputText value="#{registration.operationMessage}" style="color:green;"/>
    </h:panelGrid>
</h:form>

To render the page, start the server and navigate to:

http://localhost:8080/jsf/index.jsf

We can also use EL in the JSF view, to access the Spring bean. To test it it is enough to change the line number 7 from the previously introduced JSF page to:

<h:commandButton value="Save"
  action="#{registration.userDao.createUser(userName.value)}"/>

Here, we call the createUser method directly on the Spring DAO, passing the bind value of the userName to the method from within the JSF page, circumventing the managed bean all together.

5. Conclusion

We examined a basic integration between the Spring and JSF contexts, where we’re able to access a Spring bean in a JSF bean and page.

It’s worth noting that while the JSF runtime provides the pluggable architecture that enables the Spring framework to provide integration components, the annotations from the Spring framework cannot be used in a JSF context and vice versa.

What this means is that you’ll not be able to use annotations like @Autowired or @Component e.t.c. in a JSF managed bean, or use the @ManagedBean annotation on a Spring managed bean. You can however, use the @Inject annotation in both a JSF 2.2+ managed bean, and a Spring bean (because Spring supports JSR-330).

The source code that accompanies this article is available at GitHub.

Get the early-bird price (20% Off) of my upcoming "Learn Spring Security" Course:

>> CHECK OUT THE COURSE


Viewing all articles
Browse latest Browse all 4536

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>