You usually set up your web application as you would do with any other JSF web application. The following sample configuration files show you how to enable the above mentioned features.
The web.xml configuration:
WEB-INF/web.xml
<?xml version="1.0"?>
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<filter>
<filter-name>RequestHandled</filter-name>
<filter-class>de.mindmatters.faces.spring.RequestHandledFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>RequestHandled</filter-name>
<url-pattern>/faces/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>FacesServlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>FacesServlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
</web-app>
A web.xml configuration with net.sourceforge.myfaces JSF-implementation 1.0.2 (because an internal spring-based VariableResolver is used a few features which are not supported by MyFaces yet, e.g. list-entries, map-entries..., are enabled):
WEB-INF/web.xml
<?xml version="1.0"?>
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<filter>
<filter-name>RequestHandled</filter-name>
<filter-class>org.springframework.web.jsf.RequestHandledFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>RequestHandled</filter-name>
<url-pattern>/faces/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>net.sourceforge.myfaces.webapp.StartupServletContextListener</listener-class>
</listener>
<servlet>
<servlet-name>FacesServlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>FacesServlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
</web-app>
Example
In this example, we just use two beans:
example.NameBean
with one property:
and two methods that receive JSF events:
public void action(ActionEvent event)
public void valueChanged(ValueChangeEvent event)
example.ReferencingBean
with one property:
WEB-INF/faces-config.xml (partial)
<managed-bean>
<managed-bean-name>jsfBean</managed-bean-name>
<managed-bean-class>example.NameBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
<managed-property>
<property-name>name</property-name>
</managed-property>
</managed-bean>
<managed-bean>
<managed-bean-name>scopedAccessSpringBean</managed-bean-name>
<managed-bean-class>de.mindmatters.faces.spring.SpringBeanFactory</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<managed-bean>
<managed-bean-name>referencingBean</managed-bean-name>
<managed-bean-class>example.ReferencingBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
<managed-property>
<property-name>referencedBean</property-name>
<value>#{managedPropertyAccessSpringBean}</value>
</managed-property>
</managed-bean>
WEB-INF/applicationContext.xml (partial)
<bean id="directAccessSpringBean" class="example.NameBean"/>
<bean id="scopedAccessSpringBean" class="example.NameBean" singleton="false"/>
<bean id="managedPropertyAccessSpringBean" class="example.NameBean" singleton="false"/>
test.jsp
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<html>
<head><title>test</title></head>
<body bgcolor="white">
<f:view>
<h:messages/>
<h:form>
purely JSF managed bean:
<h:inputText value="#{jsfBean.name}"
valueChangeListener="#{jsfBean.valueChanged}"/>
<h:commandButton value="action"
actionListener="#{jsfBean.action}"/>
<br/>
purely Spring managed bean:
<h:inputText value="#{directAccessSpringBean.name}"
valueChangeListener="#{directAccessSpringBean.valueChanged}"/>
<h:commandButton value="action"
actionListener="#{directAccessSpringBean.action}"/>
<br/>
scoped Spring managed bean:
<h:inputText value="#{scopedAccessSpringBean.name}"
valueChangeListener="#{scopedAccessSpringBean.valueChanged}"/>
<h:commandButton value="action"
actionListener="#{scopedAccessSpringBeanaction}"/>
<br/>
referenced Spring managed bean:
<h:inputText value="#{referencingBean.referencedBean.name}"
valueChangeListener="#{referencingBean.referencedBean.valueChanged}"/>
<h:commandButton value="action"
actionListener="#{referencingBean.referencedBean.action}"/>
<br/>
<h:commandButton id="submit" action="save" value="save"/>
</h:form>
</f:view>
</body>
</html>