JavaServer Faces (JSF) is the JCP's standard component-based, event-driven web user interface framework. As of Java EE 5, it is an official part of the Java EE umbrella.
For a popular JSF runtime as well as for popular JSF component libraries, check out the Apache MyFaces project. The MyFaces project also provides common JSF extensions such as MyFaces Orchestra: a Spring-based JSF extension that provides rich conversation scope support.
![]() | Note |
|---|---|
Spring Web Flow 2.0 provides rich JSF support through its newly established Spring Faces module, both for JSF-centric usage (as described in this section) and for Spring-centric usage (using JSF views within a Spring MVC dispatcher). Check out the Spring Web Flow website for details! |
The key element in Spring's JSF integration is the JSF 1.1
VariableResolver mechanism. On JSF 1.2, Spring
supports the ELResolver mechanism as a
next-generation version of JSF EL integration.
The easiest way to integrate one's Spring middle-tier with one's
JSF web layer is to use the
DelegatingVariableResolver class. To
configure this variable resolver in one's application, one will need to
edit one's faces-context.xml file. After the
opening <faces-config/> element, add an
<application/> element and a
<variable-resolver/> element within it. The
value of the variable resolver should reference Spring's
DelegatingVariableResolver; for example:
<faces-config> <application> <variable-resolver>org.springframework.web.jsf.DelegatingVariableResolver</variable-resolver> <locale-config> <default-locale>en</default-locale> <supported-locale>en</supported-locale> <supported-locale>es</supported-locale> </locale-config> <message-bundle>messages</message-bundle> </application> </faces-config>
The DelegatingVariableResolver will first
delegate value lookups to the default resolver of the underlying JSF
implementation, and then to Spring's 'business context'
WebApplicationContext. This allows one to easily
inject dependencies into one's JSF-managed beans.
Managed beans are defined in one's
faces-config.xml file. Find below an example where
#{userManager} is a bean that is retrieved from the
Spring 'business context'.
<managed-bean> <managed-bean-name>userList</managed-bean-name> <managed-bean-class>com.whatever.jsf.UserList</managed-bean-class> <managed-bean-scope>request</managed-bean-scope> <managed-property> <property-name>userManager</property-name> <value>#{userManager}</value> </managed-property> </managed-bean>
SpringBeanVariableResolver is a variant of
DelegatingVariableResolver. It delegates to the
Spring's 'business context' WebApplicationContext
first, then to the default resolver of the
underlying JSF implementation. This is useful in particular when using
request/session-scoped beans with special Spring resolution rules, e.g.
Spring FactoryBean
implementations.
Configuration-wise, simply define
SpringBeanVariableResolver in your
faces-context.xml file:
<faces-config> <application> <variable-resolver>org.springframework.web.jsf.SpringBeanVariableResolver</variable-resolver> ... </application> </faces-config>
SpringBeanFacesELResolver is a JSF 1.2
compliant ELResolver implementation, integrating
with the standard Unified EL as used by JSF 1.2 and JSP 2.1. Like
SpringBeanVariableResolver, it delegates to the
Spring's 'business context' WebApplicationContext
first, then to the default resolver of the
underlying JSF implementation.
Configuration-wise, simply define
SpringBeanFacesELResolver in your JSF 1.2
faces-context.xml file:
<faces-config> <application> <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver> ... </application> </faces-config>
A custom VariableResolver works
well when mapping one's properties to beans in
faces-config.xml, but at times one may need to grab
a bean explicitly. The
FacesContextUtils class makes this easy.
It is similar to WebApplicationContextUtils,
except that it takes a FacesContext parameter
rather than a ServletContext parameter.
ApplicationContext ctx = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());