{"id":216,"date":"2009-04-27T22:07:31","date_gmt":"2009-04-28T03:07:31","guid":{"rendered":"http:\/\/www.andygibson.net\/blog\/?p=216"},"modified":"2010-08-09T14:23:53","modified_gmt":"2010-08-09T19:23:53","slug":"creating-a-spring-web-flow-jsf-project-from-scratch","status":"publish","type":"post","link":"https:\/\/www.andygibson.net\/blog\/tutorial\/creating-a-spring-web-flow-jsf-project-from-scratch\/","title":{"rendered":"Creating A Spring Web Flow JSF Project From Scratch"},"content":{"rendered":"<p>(<b>Updated &#8211; 9 August 2010<\/b> &#8211; This was written in my pre-Maven days and after a few requests for working source, I&#8217;ve built the same project using Maven which can be <a href='http:\/\/www.andygibson.net\/blog\/wp-content\/uploads\/2009\/04\/swfproject.zip'>downloaded<\/a>. Just unzip the maven project, go to the directory in the command line and type <code>mvn jetty:run<\/code> to start the server and deploy the project. Navigate to <a href=\"http:\/\/localhost:8080\/swfproject\/home.jsf\">http:\/\/localhost:8080\/swfproject\/home.jsf<\/a> or <a href=\"http:\/\/localhost:8080\/swfproject\/spring\/testFlow\">http:\/\/localhost:8080\/swfproject\/spring\/testFlow<\/a> to see the pages demonstrated in the tutorial.<\/p>\n<p>I recently had to start another project using Spring Web Flow and found myself banging my head against a brick wall to get the web flow stuff set up and to request the page properly. As a result, I decided to write up my results as a quick how-to for other developers should they find themselves in the same situation and also as a reference for myself the next time I need to start a Spring Web Flow project using Spring Faces from scratch.This article is meant more of a &#8220;here&#8217;s-how&#8221; as opposed to a &#8220;how-to&#8221; or an &#8220;explain-why&#8221; so we&#8217;ll move at a quick pace with little explanation.<br \/>\n<!--more--><br \/>\nFor the IDE, I used Eclipse 3.4.1 with Spring IDE plugins version 2.2.1, with Spring 2.5.6 (with dependencies) and Spring Web Flow 2.0.5. You should be able to use SWF 2.0.7 without any problems. For dependencies, I mostly used the ones provided with Spring except for a few, the details of which are included below. Hibernate was used as the JPA implementation and it was all deployed on Tomcat 6.0.18.<\/p>\n<h1>Getting Started<\/h1>\n<p>U started by installing Eclipse and then the Spring plugins. I created a new workspace, and added Tomcat as a server, and created a new dynamic web project. I right clicked on the project to add the Spring nature to the project.<br \/>\nThe project will be arranged with multiple Spring configuration files for the database, spring web flow and the main <code>applicationContext.xml<\/code> to include them in. Flows will be in a directory under <code>\/WebContent\/WEB-INF\/flows<\/code>.<br \/>\nI started off setting <code>web.xml<\/code> up with the faces pieces and the Spring MVC dispatcher servlet. <\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n\t&lt;!-- The main config file for this Spring web application --&gt;\r\n\t&lt;context-param&gt;\r\n\t\t&lt;param-name&gt;contextConfigLocation&lt;\/param-name&gt;\r\n\t\t&lt;param-value&gt;\/WEB-INF\/applicationContext.xml&lt;\/param-value&gt;\r\n\t&lt;\/context-param&gt;\r\n\r\n\t&lt;!-- Use JSF view templates saved as *.xhtml, for use with Facelets --&gt;\r\n\t&lt;context-param&gt;\r\n\t\t&lt;param-name&gt;javax.faces.DEFAULT_SUFFIX&lt;\/param-name&gt;\r\n\t\t&lt;param-value&gt;.xhtml&lt;\/param-value&gt;\r\n\t&lt;\/context-param&gt;\r\n\r\n\t&lt;!-- Enables special Facelets debug output during development --&gt;\r\n\t&lt;context-param&gt;\r\n\t\t&lt;param-name&gt;facelets.DEVELOPMENT&lt;\/param-name&gt;\r\n\t\t&lt;param-value&gt;true&lt;\/param-value&gt;\r\n\t&lt;\/context-param&gt;\r\n\r\n\t&lt;!-- Causes Facelets to refresh templates during development --&gt;\r\n\t&lt;context-param&gt;\r\n\t\t&lt;param-name&gt;facelets.REFRESH_PERIOD&lt;\/param-name&gt;\r\n\t\t&lt;param-value&gt;1&lt;\/param-value&gt;\r\n\t&lt;\/context-param&gt;\r\n\r\n\t&lt;!-- Loads the Spring web application context --&gt;\r\n\t&lt;listener&gt;\r\n\t\t&lt;listener-class&gt;\r\n\t\t\torg.springframework.web.context.ContextLoaderListener\r\n\t\t&lt;\/listener-class&gt;\r\n\t&lt;\/listener&gt;\r\n\r\n\t&lt;!-- Serves static resource content from .jar files such as spring-faces.jar --&gt;\r\n\t&lt;servlet&gt;\r\n\t\t&lt;servlet-name&gt;Resources Servlet&lt;\/servlet-name&gt;\r\n\t\t&lt;servlet-class&gt;\r\n\t\t\torg.springframework.js.resource.ResourceServlet\r\n\t\t&lt;\/servlet-class&gt;\r\n\t\t&lt;load-on-startup&gt;0&lt;\/load-on-startup&gt;\r\n\t&lt;\/servlet&gt;\r\n\r\n\t&lt;!-- Map all \/resources requests to the Resource Servlet for handling --&gt;\r\n\t&lt;servlet-mapping&gt;\r\n\t\t&lt;servlet-name&gt;Resources Servlet&lt;\/servlet-name&gt;\r\n\t\t&lt;url-pattern&gt;\/resources\/*&lt;\/url-pattern&gt;\r\n\t&lt;\/servlet-mapping&gt;\r\n\r\n\t&lt;!-- The front controller of this Spring Web application, responsible for handling all application requests --&gt;\r\n\t&lt;servlet&gt;\r\n\t\t&lt;servlet-name&gt;Spring MVC Dispatcher Servlet&lt;\/servlet-name&gt;\r\n\t\t&lt;servlet-class&gt;\r\n\t\t\torg.springframework.web.servlet.DispatcherServlet\r\n\t\t&lt;\/servlet-class&gt;\r\n\t\t&lt;init-param&gt;\r\n\t\t\t&lt;param-name&gt;contextConfigLocation&lt;\/param-name&gt;\r\n\t\t\t&lt;param-value&gt;&lt;\/param-value&gt;\r\n\t\t&lt;\/init-param&gt;\r\n\t\t&lt;load-on-startup&gt;1&lt;\/load-on-startup&gt;\r\n\t&lt;\/servlet&gt;\r\n\r\n\t&lt;!-- Map all \/spring requests to the Dispatcher Servlet for handling --&gt;\r\n\t&lt;servlet-mapping&gt;\r\n\t\t&lt;servlet-name&gt;Spring MVC Dispatcher Servlet&lt;\/servlet-name&gt;\r\n\t\t&lt;url-pattern&gt;\/spring\/*&lt;\/url-pattern&gt;\r\n\t&lt;\/servlet-mapping&gt;\r\n\r\n\t&lt;!-- Just here so the JSF implementation can initialize, *not* used at runtime --&gt;\r\n\t&lt;servlet&gt;\r\n\t\t&lt;servlet-name&gt;Faces Servlet&lt;\/servlet-name&gt;\r\n\t\t&lt;servlet-class&gt;javax.faces.webapp.FacesServlet&lt;\/servlet-class&gt;\r\n\t\t&lt;load-on-startup&gt;1&lt;\/load-on-startup&gt;\r\n\t&lt;\/servlet&gt;\r\n\r\n\t&lt;!-- Just here so the JSF implementation can initialize --&gt;\r\n\t&lt;servlet-mapping&gt;\r\n\t\t&lt;servlet-name&gt;Faces Servlet&lt;\/servlet-name&gt;\r\n\t\t&lt;url-pattern&gt;*.jsf&lt;\/url-pattern&gt;\r\n\t&lt;\/servlet-mapping&gt;\r\n\r\n\t&lt;welcome-file-list&gt;\r\n\t\t&lt;welcome-file&gt;index.html&lt;\/welcome-file&gt;\r\n\t&lt;\/welcome-file-list&gt;\r\n\t\r\n<\/pre>\n<p>At the top of this <code>web.xml<\/code> file, we indicate that our primary Spring bean config file is called <code>\\WEB-INF\\applicationContext.xml<\/code> so we navigate to that folder (it&#8217;s in the WebContent folder) and right click and add a new Spring Bean Definition. We also add two other Spring Bean definitions in the same place called <code>dbConfig.xml<\/code> and <code>flowConfig.xml<\/code>. These files are defined below :<\/p>\n<p><code>\/WebContent\/WEB-INF\/applicationContext.xml<\/code> <\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;\r\n&lt;beans xmlns=&quot;http:\/\/www.springframework.org\/schema\/beans&quot;\r\n\txmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot;\r\n\txsi:schemaLocation=&quot;http:\/\/www.springframework.org\/schema\/beans http:\/\/www.springframework.org\/schema\r\n\/beans\/spring-beans.xsd&quot;&gt;\r\n\r\n\t&lt;import resource=&quot;dbConfig.xml&quot; \/&gt;\r\n\t&lt;import resource=&quot;flowConfig.xml&quot; \/&gt;\r\n&lt;\/beans&gt;\r\n<\/pre>\n<p><code>\/WebContent\/WEB-INF\/dbConfig.xml<\/code> <\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;\r\n&lt;beans xmlns=&quot;http:\/\/www.springframework.org\/schema\/beans&quot;\r\n\txmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot;\r\n\txmlns:aop=&quot;http:\/\/www.springframework.org\/schema\/aop&quot;\r\n\txmlns:tx=&quot;http:\/\/www.springframework.org\/schema\/tx&quot;\r\n\txsi:schemaLocation=&quot;http:\/\/www.springframework.org\/schema\/beans http:\/\/www.springframework.org\/schema\/beans\/spring-beans.xsd\r\n\t\thttp:\/\/www.springframework.org\/schema\/aop http:\/\/www.springframework.org\/schema\/aop\/spring-aop.xsd\r\n\t\thttp:\/\/www.springframework.org\/schema\/tx http:\/\/www.springframework.org\/schema\/tx\/spring-tx.xsd&quot;&gt;\r\n\r\n\t&lt;bean id=&quot;entityManagerFactory&quot;\r\n\tclass=&quot;org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean&quot;&gt;\r\n\t\t&lt;property name=&quot;jpaVendorAdapter&quot;&gt;\r\n\t\t\t&lt;bean\r\n\tclass=&quot;org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter&quot;&gt;\r\n\t\t\t\t&lt;property name=&quot;showSql&quot; value=&quot;true&quot; \/&gt;\r\n\t\t\t\t&lt;property name=&quot;generateDdl&quot; value=&quot;true&quot; \/&gt;\r\n\t\t\t\t&lt;property name=&quot;databasePlatform&quot;\r\n\tvalue=&quot;org.hibernate.dialect.MySQLDialect&quot; \/&gt;\r\n\t\t\t&lt;\/bean&gt;\r\n\t\t&lt;\/property&gt;\r\n\t\t&lt;property name=&quot;dataSource&quot; ref=&quot;dataSource&quot; \/&gt;\r\n\t\t\r\n\t&lt;\/bean&gt;\r\n\r\n\t&lt;bean id=&quot;dataSource&quot; class=&quot;org.apache.commons.dbcp.BasicDataSource&quot;\r\n\tdestroy-method=&quot;close&quot;&gt;\r\n\t\t&lt;property name=&quot;driverClassName&quot; value=&quot;com.mysql.jdbc.Driver&quot; \/&gt;\r\n\t\t&lt;property name=&quot;url&quot; value=&quot;jdbc:mysql:\/\/localhost:3306\/dbName&quot; \/&gt;\r\n\t\t&lt;property name=&quot;username&quot; value=&quot;someUser&quot; \/&gt;\r\n\t\t&lt;property name=&quot;password&quot; value=&quot;somePassword&quot; \/&gt;\r\n\t&lt;\/bean&gt;\r\n\r\n\t&lt;bean id=&quot;transactionManager&quot;\r\n\tclass=&quot;org.springframework.orm.jpa.JpaTransactionManager&quot;&gt;\r\n\t\t&lt;property name=&quot;dataSource&quot; ref=&quot;dataSource&quot; \/&gt;\r\n\t\t&lt;property name=&quot;entityManagerFactory&quot; ref=&quot;entityManagerFactory&quot; \/&gt;\r\n\t&lt;\/bean&gt;\r\n\r\n\t&lt;tx:annotation-driven \/&gt;\r\n\r\n\t&lt;bean\r\n\tclass=&quot;org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor&quot; \/&gt;\r\n\r\n&lt;\/beans&gt;\r\n\r\n<\/pre>\n<p><code>\/WebContent\/WEB-INF\/flowConfig.xml<\/code> <\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;\r\n&lt;beans xmlns=&quot;http:\/\/www.springframework.org\/schema\/beans&quot;\r\n\txmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot;\r\n\txmlns:webflow=&quot;http:\/\/www.springframework.org\/schema\/webflow-config&quot;\r\n\txmlns:faces=&quot;http:\/\/www.springframework.org\/schema\/faces&quot;\r\n\txsi:schemaLocation=&quot;\r\n           http:\/\/www.springframework.org\/schema\/beans\r\n           http:\/\/www.springframework.org\/schema\/beans\/spring-beans-2.5.xsd\r\n           http:\/\/www.springframework.org\/schema\/webflow-config\r\n           http:\/\/www.springframework.org\/schema\/webflow-config\/spring-webflow-config-2.0.xsd\r\n           http:\/\/www.springframework.org\/schema\/faces\r\n           http:\/\/www.springframework.org\/schema\/faces\/spring-faces-2.0.xsd&quot;&gt;\r\n\r\n\t&lt;!--Executes flows: the central entry point into the Spring Web Flow system--&gt;\r\n\t&lt;webflow:flow-executor id=&quot;flowExecutor&quot;&gt;\r\n\t\t&lt;webflow:flow-execution-listeners&gt;\r\n\t\t\t&lt;webflow:listener ref=&quot;jpaFlowExecutionListener&quot; \/&gt;\r\n\r\n\t\t&lt;\/webflow:flow-execution-listeners&gt;\r\n\t&lt;\/webflow:flow-executor&gt;\r\n\r\n\t&lt;!-- The registry of executable flow definitions --&gt;\r\n\t&lt;webflow:flow-registry id=&quot;flowRegistry&quot;\r\n\tflow-builder-services=&quot;facesFlowBuilderServices&quot;&gt;\r\n\t\t&lt;webflow:flow-location path=&quot;\/WEB-INF\/flows\/testFlow\/testFlow.xml&quot;&gt;&lt;\/webflow:flow-location&gt;\r\n\t&lt;\/webflow:flow-registry&gt;\r\n\r\n\t&lt;!-- Configures the Spring Web Flow JSF integration --&gt;\r\n\t&lt;faces:flow-builder-services id=&quot;facesFlowBuilderServices&quot;\r\n\tdevelopment=&quot;true&quot; \/&gt;\r\n\r\n\t&lt;!--\r\n\t\tInstalls a listener that manages JPA persistence contexts for flows\r\n\t\tthat require them\r\n\t--&gt;\r\n\t&lt;bean id=&quot;jpaFlowExecutionListener&quot;\r\n\tclass=&quot;org.springframework.webflow.persistence.JpaFlowExecutionListener&quot;&gt;\r\n\t\t&lt;constructor-arg ref=&quot;entityManagerFactory&quot; \/&gt;\r\n\t\t&lt;constructor-arg ref=&quot;transactionManager&quot; \/&gt;\r\n\t&lt;\/bean&gt;\r\n\r\n\t&lt;!-- Maps request URIs to controllers --&gt;\r\n\t&lt;bean\r\n\tclass=&quot;org.springframework.web.servlet.handler.SimpleUrlHandlerMapping&quot;&gt;\r\n\r\n\t\t&lt;property name=&quot;mappings&quot;&gt;\r\n\r\n\t\t\t&lt;value&gt;\r\n\t\t\t\t\/testFlow=flowController\r\n\t\t\t&lt;\/value&gt;\r\n\t\t&lt;\/property&gt;\r\n\t\t&lt;property name=&quot;defaultHandler&quot;&gt;\r\n\t\t\t&lt;!--\r\n\t\t\t\tSelects view names to render based on the request URI: e.g. \/main\r\n\t\t\t\tselects &quot;main&quot;\r\n\t\t\t--&gt;\r\n\t\t\t&lt;bean\r\n\tclass=&quot;org.springframework.web.servlet.mvc.UrlFilenameViewController&quot; \/&gt;\r\n\t\t&lt;\/property&gt;\r\n\t&lt;\/bean&gt;\r\n\r\n\t&lt;!-- Handles requests mapped to the Spring Web Flow system --&gt;\r\n\t&lt;bean id=&quot;flowController&quot;\r\n\tclass=&quot;org.springframework.webflow.mvc.servlet.FlowController&quot;&gt;\r\n\t\t&lt;property name=&quot;flowExecutor&quot; ref=&quot;flowExecutor&quot; \/&gt;\r\n\t\t\r\n\t&lt;\/bean&gt;\r\n\r\n&lt;\/beans&gt;\r\n<\/pre>\n<p>Since we are using JPA, we need to include a <code>persistence.xml<\/code> file to the classpath in <code>src\/META-INF\/persistence.xml<\/code>.<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;\r\n&lt;persistence xmlns=&quot;http:\/\/java.sun.com\/xml\/ns\/persistence&quot;\r\n\txmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot;\r\n\txsi:schemaLocation=&quot;http:\/\/java.sun.com\/xml\/ns\/persistence\r\n      http:\/\/java.sun.com\/xml\/ns\/persistence\/persistence_1_0.xsd&quot;\r\n\tversion=&quot;1.0&quot;&gt;\r\n\r\n    &lt;persistence-unit name=&quot;default&quot; transaction-type=&quot;RESOURCE_LOCAL&quot; \/&gt;\r\n    \r\n&lt;\/persistence&gt;\r\n<\/pre>\n<p>Now we need to add a <code>faces-config.xml<\/code> in the same location.<\/p>\n<p><code>\/WebContent\/WEB-INF\/faces-config.xml<\/code> <\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;\r\n&lt;faces-config version=&quot;1.2&quot; xmlns=&quot;http:\/\/java.sun.com\/xml\/ns\/javaee&quot;\r\n\txmlns:xi=&quot;http:\/\/www.w3.org\/2001\/XInclude&quot;\r\n\txmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot;\r\n\txsi:schemaLocation=&quot;http:\/\/java.sun.com\/xml\/ns\/javaee http:\/\/java.sun.com\/xml\/ns\/javaee\/web-facesconfig_1_2.xsd&quot;&gt;\r\n\r\n\t&lt;application&gt;\r\n\t\t&lt;el-resolver&gt;org.springframework.web.jsf.el.SpringBeanFacesELResolver&lt;\/el-resolver&gt;\r\n\t\t&lt;view-handler&gt;com.sun.facelets.FaceletViewHandler&lt;\/view-handler&gt;\r\n\t&lt;\/application&gt;\r\n&lt;\/faces-config&gt;\r\n\r\n<\/pre>\n<p>We&#8217;ll also add a html page that redirects to a jsf page immediately. Since we are using facelets, we&#8217;ll also throw in a template to work from. In the WebContent folder, we&#8217;ll add the <code>templates<\/code> directory to contain our page layout.<\/p>\n<p><code>WebContent\\templates\\template.xhtml<\/code> <\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;!DOCTYPE html PUBLIC &quot;-\/\/W3C\/\/DTD XHTML 1.0 Transitional\/\/EN&quot; \r\n                      &quot;http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-transitional.dtd&quot;&gt;\r\n&lt;html xmlns=&quot;http:\/\/www.w3.org\/1999\/xhtml&quot;\r\n\txmlns:ui=&quot;http:\/\/java.sun.com\/jsf\/facelets&quot;\r\n\txmlns:h=&quot;http:\/\/java.sun.com\/jsf\/html&quot;\r\n\txmlns:f=&quot;http:\/\/java.sun.com\/jsf\/core&quot;&gt;\r\n&lt;head&gt;\r\n  &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text\/html; charset=UTF-8&quot; \/&gt;\r\n\r\n  &lt;title&gt;&lt;ui:insert name=&quot;title&quot;&gt;untitled&lt;\/ui:insert&gt;&lt;\/title&gt;\r\n\r\n&lt;\/head&gt;\r\n\r\n&lt;body&gt;\r\n  &lt;h:messages globalOnly=&quot;true&quot; \/&gt;\r\n\r\n  &lt;ui:insert name=&quot;title&quot;&gt;Title Here&lt;\/ui:insert&gt;\r\n\r\n  &lt;ui:insert name=&quot;body&quot; \/&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p>Now to add our two pages that will initially launch is into a JSF page.<\/p>\n<p><code>\/WebContent\/index.html<\/code><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n  &lt;meta http-equiv=&quot;Refresh&quot; content=&quot;0; URL=home.jsf&quot;&gt;\r\n&lt;\/head&gt;\r\n&lt;\/html&gt;\r\n&lt;\/textarea&gt; &lt;code&gt;\/WebContent\/home.xhtml&lt;\/code&gt; &lt;pre name=&quot;code&quot; class=&quot;xml&quot;&gt;\r\n&lt;!DOCTYPE composition PUBLIC &quot;-\/\/W3C\/\/DTD XHTML 1.0 Transitional\/\/EN&quot; \r\n                      &quot;http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-transitional.dtd&quot;&gt;\r\n&lt;ui:composition xmlns=&quot;http:\/\/www.w3.org\/1999\/xhtml&quot;\r\n\txmlns:ui=&quot;http:\/\/java.sun.com\/jsf\/facelets&quot;\r\n\txmlns:f=&quot;http:\/\/java.sun.com\/jsf\/core&quot;\r\n\txmlns:h=&quot;http:\/\/java.sun.com\/jsf\/html&quot;\r\n\ttemplate=&quot;templates\/template.xhtml&quot;&gt;\r\n\r\n\t&lt;ui:define name=&quot;body&quot;&gt;\r\n\t\t&lt;h:form&gt;\r\n\r\n\t\t\t&lt;h:outputText value=&quot;Hello From JSF!&quot; \/&gt;\r\n\r\n\t\t&lt;\/h:form&gt;\r\n\t&lt;\/ui:define&gt;\r\n&lt;\/ui:composition&gt;\r\n\r\n<\/pre>\n<p>One more configuration file we need is for Log4j to get rid of the warnings that it has not been set up correctly. The properties file goes in the <code>src<\/code> directory.<\/p>\n<p><code>\/src\/log4j.properties<\/code><\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n### direct log messages to stdout ###\r\nlog4j.appender.stdout=org.apache.log4j.ConsoleAppender\r\nlog4j.appender.stdout.Target=System.out\r\nlog4j.appender.stdout.layout=org.apache.log4j.PatternLayout\r\nlog4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n\r\nlog4j.rootLogger=info, stdout\r\nlog4j.category.org.springframework=WARN\r\nlog4j.category.org.hibernate=WARN\r\n<\/pre>\n<p>Now let&#8217;s look at libraries. There are a bunch of libraries that are needed since we haven&#8217;t added any yet.<\/p>\n<table style=\"font-family: sans-serif; font-size: 12px\">\n<tr>\n<th colspan=\"2\">Libraries located in <code>\/WebContent\/WEB-INF\/lib\/<\/code> <\/th>\n<\/tr>\n<tr>\n<td>antlr-2.7.6.jar<\/td>\n<td>Spring Dependencies<\/td>\n<\/tr>\n<tr>\n<td>cglib-nodep-2.1_3.jar<\/td>\n<td>Spring Dependencies<\/td>\n<\/tr>\n<tr>\n<td>commons-beanutils.jar<\/td>\n<td>Spring Dependencies<\/td>\n<\/tr>\n<tr>\n<td>commons-collections.jar<\/td>\n<td>Spring Dependencies<\/td>\n<\/tr>\n<tr>\n<td>commons-dbcp.jar<\/td>\n<td>Spring Dependencies<\/td>\n<\/tr>\n<tr>\n<td>commons-digester.jar<\/td>\n<td>Spring Dependencies<\/td>\n<\/tr>\n<tr>\n<td>commons-logging.jar<\/td>\n<td>Spring Dependencies<\/td>\n<\/tr>\n<tr>\n<td>commons-pool.jar<\/td>\n<td>Spring Dependencies<\/td>\n<\/tr>\n<tr>\n<td>dom4j-1.6.1.jar<\/td>\n<td>Spring Dependencies<\/td>\n<\/tr>\n<tr>\n<td>hibernate3.jar<\/td>\n<td>Spring Dependencies<\/td>\n<\/tr>\n<tr>\n<td>hibernate-annotations.jar<\/td>\n<td>Spring Dependencies<\/td>\n<\/tr>\n<tr>\n<td>hibernate-commons-annotations.jar<\/td>\n<td>Spring Dependencies<\/td>\n<\/tr>\n<tr>\n<td>hibernate-entitymanager.jar<\/td>\n<td>Spring Dependencies<\/td>\n<\/tr>\n<tr>\n<td>javaee.jar<\/td>\n<td>Obtained From Glassfish<\/td>\n<\/tr>\n<tr>\n<td>javassist-3.4.GA.jar<\/td>\n<td>Spring Dependencies<\/td>\n<\/tr>\n<tr>\n<td>jboss-el.jar<\/td>\n<td>Obtained From JBoss Seam<\/td>\n<\/tr>\n<tr>\n<td>jsf-api.jar<\/td>\n<td>Downloaded Mojarra 1.2_11<\/td>\n<\/tr>\n<tr>\n<td>jsf-facelets.jar<\/td>\n<td>Obtained From JBoss Seam<\/td>\n<\/tr>\n<tr>\n<td>jsf-impl.jar &#8211;<\/td>\n<td>Downloaded Mojarra 1.2_11<\/td>\n<\/tr>\n<tr>\n<td>log4j-1.2.15.jar<\/td>\n<td>Spring Dependencies<\/td>\n<\/tr>\n<tr>\n<td>mysql-connector-java-5.0.4-bin.jar<\/td>\n<td>Downloaded from MySQL<\/td>\n<\/tr>\n<tr>\n<td>org.springframework.binding-2.0.5.RELEASE.jar<\/td>\n<td>Spring Web Flow Download<\/td>\n<\/tr>\n<tr>\n<td>org.springframework.faces-2.0.5.RELEASE.jar<\/td>\n<td>Spring Web Flow Download<\/td>\n<\/tr>\n<tr>\n<td>org.springframework.js-2.0.5.RELEASE.jar<\/td>\n<td>Spring Web Flow Download<\/td>\n<\/tr>\n<tr>\n<td>org.springframework.webflow-2.0.5.RELEASE.jar<\/td>\n<td>Spring Web Flow Download<\/td>\n<\/tr>\n<tr>\n<td>persistence.jar<\/td>\n<td>Spring Dependencies<\/td>\n<\/tr>\n<tr>\n<td>slf4j-api-1.5.0.jar<\/td>\n<td>Spring Dependencies<\/td>\n<\/tr>\n<tr>\n<td>slf4j-log4j12-1.5.0.jar<\/td>\n<td>Spring Dependencies<\/td>\n<\/tr>\n<tr>\n<td>spring.jar<\/td>\n<td>Spring Dependencies<\/td>\n<\/tr>\n<tr>\n<td>spring-webmvc.jar<\/td>\n<td>Spring Dependencies<\/td>\n<\/tr>\n<\/table>\n<p>One reason I downloaded the latest JSF version was because I was having problems with the JSF version I was using. The SpringBeanELResolver was being ignored at run-time, it didn&#8217;t even blink if I set it to an undefined class name, however the Spring Delegating variable resolver was working, but the IDE was saying it was deprecated. Once I upgraded to JSF 1.2_11, the EL resolver worked fine. I&#8217;m wondering if an old 1.1 JSF version had crept in there.<\/p>\n<p>Depending on where you end up deploying your application (i.e.Glassfish), you may end up having to remove some of these libraries if they are already installed on the server. In this case, I was using Apache 6.0.18 with a clean install, and therefore with no libraries added to the server.<\/p>\n<h1>Creating a Flow<\/h1>\n<p>Now we should have a working application all ready to go. To test this, we&#8217;ll add a little code and a test page just to verify that everything is working ok. Create a new class called <code>MessageHolder<\/code> in a package called <code>swfproject<\/code>. This is a simple class that contains a string that can be set and retrieved from our pages.<\/p>\n<p><code>\/src\/swfproject\/MessageHolder.java<\/code> <\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage swfproject;\r\n\r\npublic class MessageHolder implements Serializable {\r\n\r\n\tprivate String text = &quot;Hello From the Message Holder&quot;;\r\n\t\r\n\tpublic String getText() {\r\n\t\treturn text;\r\n\t}\r\n\t\r\n\tpublic void setText(String text) {\r\n\t\tthis.text = text;\r\n\t}\r\n}\r\n<\/pre>\n<p>Now we define the bean in the <code>\/WEB-INF\/applicationContext.xml<\/code> so we can call the bean from a regular jsf page, or a flow page. <\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n\t&lt;bean name=&quot;springMessage&quot; class=&quot;swfproject.MessageHolder&quot;&gt;\r\n\t\t&lt;property name=&quot;text&quot; value=&quot;This was defined in Spring&quot; \/&gt;\t\r\n\t&lt;\/bean&gt;\r\n<\/pre>\n<p>In the <code>home.xhtml<\/code> page, we add the following line to the page, somewhere between the <code>ui:define<\/code> tag on the page to display the message<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nMessage is : #{springMessage.text}\r\n<\/pre>\n<p>If you start Tomcat, assuming you have already attached the project to the server if you are using an IDE, and go to <code>http:\/\/localhost:8080\/projectName\/<\/code> you should redirect to <code>home.jsf<\/code> and it should the message that was defined in Spring.<\/p>\n<p>Now let&#8217;s add a simple flow called testFlow. If you look in the <code>flowConfig.xml<\/code> file, there is already a mappings property where we already defined <code>\/testFlow=flowController<\/code>. This means that when this url is requested, the <code>flowController<\/code> bean instance deals with it.<\/p>\n<p>We need to create a new directory under <code>\/WebContent\/WEB-INF\/flows\/<\/code>, and in there, we need to create a directory called <code>testFlow<\/code>. In that, we add a page called <code>testFlow.xhtml<\/code> and <code>testFlow.xml<\/code>. This is the view and the flow defined respectively.<br \/>\n<code>\/WebContent\/WEB-INF\/flows\/testFlow\/testFlow.xml<\/code> Flow <\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;\r\n&lt;flow xmlns=&quot;http:\/\/www.springframework.org\/schema\/webflow&quot;\r\n\txmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot;\r\n\txmlns:faces=&quot;http:\/\/www.springframework.org\/schema\/faces&quot;\r\n\txsi:schemaLocation=&quot;http:\/\/www.springframework.org\/schema\/webflow\r\n        http:\/\/www.springframework.org\/schema\/webflow\/spring-webflow-2.0.xsd&quot;\r\n\tstart-state=&quot;testFlow&quot;&gt;\r\n\r\n\t&lt;var name=&quot;flowMessage&quot; class=&quot;swfproject.MessageHolder&quot; \/&gt;\r\n\t&lt;view-state id=&quot;testFlow&quot;&gt;\r\n\t\t&lt;transition on=&quot;post&quot; to=&quot;testFlow&quot; \/&gt;\r\n\t&lt;\/view-state&gt;\r\n&lt;\/flow&gt;\r\n<\/pre>\n<p><code>\/WebContent\/WEB-INF\/flows\/testFlow\/testFlow.xhtml<\/code> View <\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;!DOCTYPE composition PUBLIC &quot;-\/\/W3C\/\/DTD XHTML 1.0 Transitional\/\/EN&quot; \r\n                      &quot;http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-transitional.dtd&quot;&gt;\r\n&lt;ui:composition xmlns=&quot;http:\/\/www.w3.org\/1999\/xhtml&quot;\r\n\txmlns:ui=&quot;http:\/\/java.sun.com\/jsf\/facelets&quot;\r\n\txmlns:f=&quot;http:\/\/java.sun.com\/jsf\/core&quot;\r\n\txmlns:h=&quot;http:\/\/java.sun.com\/jsf\/html&quot;\r\n\ttemplate=&quot;\/templates\/template.xhtml&quot;&gt;\r\n\r\n\t&lt;ui:define name=&quot;body&quot;&gt;\r\n\t\t&lt;h:form&gt;\r\n\t\t\t&lt;h:outputText value=&quot;This is a test flow&quot; \/&gt;\r\n\t\t\t&lt;br \/&gt;\r\n\t\t\tMessage From Spring = #{springMessage.text}&lt;br \/&gt;\r\n\t\t\tMessage From Flow = #{flowMessage.text}&lt;br \/&gt;\r\n\t\t\t&lt;h:inputText value=&quot;#{flowMessage.text}&quot; style=&quot;width:200px&quot; \/&gt;\r\n\t\t\t&lt;h:commandButton action=&quot;post&quot; value=&quot;Update&quot; \/&gt;\r\n \t\t&lt;\/h:form&gt;\r\n\t&lt;\/ui:define&gt;\r\n&lt;\/ui:composition&gt;\r\n<\/pre>\n<p>Now if you go to <code><a href=\"http:\/\/localhost:8080\/app_name\/spring\/testFlow\">http:\/\/localhost:8080\/app_name\/spring\/testFlow<\/a><\/code> (<b>Remember to replace <code>app_name<\/code> with your project name<\/b>) you should see the page we have made as part of our flow. It displays the message from the <code>springMessage<\/code> instance of the <code>MessageHolder<\/code> that contains the message from spring. I now also displays the instance from the flow which contains the default message displayed since the flow variable hasn&#8217;t had the text property changed. Also, the URL should change to <code>http:\/\/localhost:8080\/app_name\/spring\/testFlow?execution=e3s1<\/code> or something similar with the execution on the end. This page demonstrates that we will have access to the spring beans, as well as access to variables defined in a flow which is where <code>flowMessage<\/code> is defined. You can edit the message and click post to change the flowMessage text value. You can open the link up in two browser windows or tabs and see how the two values can be edited independently, and the flowMessage variable is scoped to the flow in the browser.<br \/>\nFrom this point, you can go ahead and move on with the application all you like. You can change the flow mappings, put in wildcarded flow locations, even get started with trying out Spring MVC (as I plan to). Hope you find this useful as a quick start guide to getting a JSF Spring Web Flow project up and running, as well as defining flows and calling them.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>(Updated &#8211; 9 August 2010 &#8211; This was written in my pre-Maven days and after a few requests for working source, I&#8217;ve built the same project using Maven which can be downloaded. Just unzip the maven project, go to the directory in the command line and type mvn jetty:run to start the server and deploy [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[64],"tags":[6,32,19],"_links":{"self":[{"href":"https:\/\/www.andygibson.net\/blog\/wp-json\/wp\/v2\/posts\/216"}],"collection":[{"href":"https:\/\/www.andygibson.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.andygibson.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.andygibson.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.andygibson.net\/blog\/wp-json\/wp\/v2\/comments?post=216"}],"version-history":[{"count":39,"href":"https:\/\/www.andygibson.net\/blog\/wp-json\/wp\/v2\/posts\/216\/revisions"}],"predecessor-version":[{"id":1322,"href":"https:\/\/www.andygibson.net\/blog\/wp-json\/wp\/v2\/posts\/216\/revisions\/1322"}],"wp:attachment":[{"href":"https:\/\/www.andygibson.net\/blog\/wp-json\/wp\/v2\/media?parent=216"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.andygibson.net\/blog\/wp-json\/wp\/v2\/categories?post=216"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.andygibson.net\/blog\/wp-json\/wp\/v2\/tags?post=216"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}