Description:
A very simple example of Spring Web Flow 2.x. This small program will show you how a simple page flow works.
Project Structure:
Dependencies:
- org.springframework.js-2.0.5.RELEASE.jar
- spring-beans-3.2.8.RELEASE.jar
- spring-binding-2.3.3.RELEASE.jar
- spring-context-3.2.8.RELEASE.jar
- spring-core-3.2.8.RELEASE.jar
- spring-expression-3.2.8.RELEASE.jar
- spring-web-3.2.8.RELEASE.jar
- spring-webflow-2.3.2.RELEASE.jar
- spring-webmvc-3.2.8.RELEASE.jar
Server:
- JBoss V4.2
Source Code:
web.xml |
<?xml version=”1.0″ encoding=”UTF-8″?> <web-app xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns=”http://java.sun.com/xml/ns/javaee” xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd” id=”WebApp_ID” version=”2.5″> <display-name>Hello World</display-name> <servlet> <servlet-name>mvc-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/mvc-dispatcher-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>mvc-dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app> |
mvc-dispatcher-servlet.xml |
<beans xmlns=”http://www.springframework.org/schema/beans” xmlns:mvc=”http://www.springframework.org/schema/mvc” xmlns:context=”http://www.springframework.org/schema/context” xmlns:flow=”http://www.springframework.org/schema/webflow-config” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=” http://www.springframework.org/schema/webflow-config http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd “><!– Maps flow requests from DispatcherServlet to flowController –> <bean class=”org.springframework.web.servlet.handler.SimpleUrlHandlerMapping”> <property name=”mappings”> <value> /helloworld/hello.do=flowController </value> </property> <property name=”alwaysUseFullPath” value=”true” /> </bean><!– Maps a logical view name to a physical resource –> <bean id=”viewResolver” class=”org.springframework.web.servlet.view.InternalResourceViewResolver”> <property name=”prefix”> <value>/WEB-INF/jsp/</value> </property> <property name=”suffix”> <value>.jsp</value> </property> </bean> <!– SPRING WEB FLOW SETTING –><bean id=”flowController” class=”org.springframework.webflow.mvc.servlet.FlowController”> <property name=”flowExecutor” ref=”flowExecutor” /> </bean><flow:flow-executor id=”flowExecutor” flow-registry=”flowRegistry” /><!– This creates an XmlFlowRegistryFactory bean –> <flow:flow-registry id=”flowRegistry” flow-builder-services=”flowBuilderServices”> <flow:flow-location path=”/WEB-INF/flows/hello.xml” /> </flow:flow-registry><flow:flow-builder-services id=”flowBuilderServices” view-factory-creator=”viewFactoryCreator” /><bean id=”viewFactoryCreator” class=”org.springframework.webflow.mvc.builder.MvcViewFactoryCreator”> <property name=”viewResolvers”> <list> <ref bean=”viewResolver” /> </list> </property> </bean> </beans> |
hello.xml |
<?xml version=”1.0″ encoding=”UTF-8″?> <flow xmlns=”http://www.springframework.org/schema/webflow” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://www.springframework.org/schema/webflow http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd”><view-state id=”firstStepId” view=”hello/firstStep”> <transition on=”secondStep” to=”secondStepId” /> </view-state><view-state id=”secondStepId” view=”hello/secondStep”> <transition on=”firstStep” to=”firstStepId” /> <transition on=”lastStep” to=”lastStepId” /> </view-state> <end-state id=”lastStepId” view=”hello/lastStep” /> </flow> |
firstStep.jsp |
<%@ page language=”java” contentType=”text/html; charset=UTF-8″ pageEncoding=”UTF-8″%> <!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”> <html> <head> <meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″> <title>This is the first step</title> </head> <body> <h3>First</h3> <hr /> Using Link: <br /> <a href=”${flowExecutionUrl}&_eventId=secondStep”>second step</a> <hr /> Using button: <br /> <form method=”post”> <input name=”_eventId_secondStep” type=”submit” value=”second step” /> </form> </body> </html> |
secondStep.jsp |
<%@ page language=”java” contentType=”text/html; charset=UTF-8″ pageEncoding=”UTF-8″%> <!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”> <html> <head> <meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″> <title>This is the second step</title> </head> <body> <h3>Second</h3> <hr /> Using Link: <br /> <a href=”${flowExecutionUrl}&_eventId=firstStep”>first step</a> <a href=”${flowExecutionUrl}&_eventId=lastStep”>last step</a> <hr /> Using button: <br /> <form method=”post”> <input name=”_eventId_firstStep” type=”submit” value=”first step” /> <input name=”_eventId_lastStep” type=”submit” value=”last step” /> </form> </body> </html> |
lastStep.jsp |
<%@ page language=”java” contentType=”text/html; charset=UTF-8″ pageEncoding=”UTF-8″%> <!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”> <html> <head> <meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″> <title>This is the last step</title> </head> <body> <h3>Last</h3> <h2>Hello Spring WebFlow</h2> </body> </html> |
Screen Shot:
Access http://localhost:8080/SwingWebFlowHelloWorld/helloworld/hello.do on your browser: