This is not a tutorial for experts. What I am about to explain is just the basics and how to get going if you are starting with Spring web flow. You can begin with creation of a web application project using your Eclipse or Netbeans. You need to ensure that you have few jars:
- Spring Web Flow (http://www.springsource.org/download#webflow). I used the version 2.0.8
- ONGL (http://www.jarfinder.com/index.php/jars/versionInfo/433). I used version 2.6.7
- I will assume you already have the Spring framework jars (I used version 2.5.6). I also used JSTL jars
Configuring the web.xml
There is no change in web.xml compared to a Spring MVC application. I just have my DispatcherServlet configured and other necessary configurations.
<servlet> <servlet-name>swft1</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>swft1</servlet-name> <url-pattern>*.htm</url-pattern> </servlet-mapping>Configuring the application context
I have only one application context file and its named swft1-servlet.xml. Here is my configurations:
<bean id="handlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <value>/msgform.htm=flowController</value> </property> </bean> <bean id="flowController" class="org.springframework.webflow.mvc.servlet.FlowController"> <property name="flowExecutor" ref="flowExecutor" /> </bean> <flow:flow-executor id="flowExecutor" flow-registry="flowRegistry"/> <flow:flow-registry id="flowRegistry"> <flow:flow-location id="msgform" path="/WEB-INF/flows/simple-flow.xml" /> </flow:flow-registry>Just like a Spring MVC application, I have a used the SimpleUrlHandlerMapping and mapped msgform.htm to flowController. The flowController is an instance of a specialized contoller class available as part of the web flow package. we will configure it with a property named flowExecutor. Flow executor drives the execution of the flow. And its configured with the tag flow-executor. We have a flow registry that holds all the flows for the application. Flows can be defined in multiple files and a registry is maintained. A flow registry is defined using the flow-registry tag as shown above. In our example, I have a single file defining the application flow. The location if the file is defined using the flow-location tag as shown above.
This is minimal the minimal configuration required! You will notice, that there is no view resolver. We will be making use of the default view resolver here. I will further explain this as we go.
Configuring the flow
A flow is described using XML files. And the flow file has to be registered in flow registry. In this example, We have only one file named simple-flow.xml:
<var name="msgObj" class="com.tpblog.web.models.Message"/> <view-state id="start" model="msgObj" view="message.jsp"> <transition on="sendAction" to="success-state" /> </view-state> <end-state id="success-state" view="success.jsp" commit="true" />Please note that I have removed the root element of the xml. I have only used few features to keep it simple. Here we get a message from one screen and its displayed in another (web flow is NOT for these kinda stuff!!!). I have a msgObj that holds the message we type in the first screen (message.jsp). On clicking the submit button, we display success.jsp with the message.
In this flow, I have just two states; a view state and an end state. Notice that, I have specified the JSP files in the view attribute. Since,I am not using any view resolver, I need to specify the complete file name. The JSP files should also reside in the same folder as the flow configuration file. Once you have the basic application running, you can add features to it and modify it with other configurations.
For demo, I have a simple example of user login.In the next post, we will have a look at how to configure the view resolvers.