Spring Web Flow Introduction Demo (a) attached source

Source: Internet
Author: User
Tags unique id


Spring Web Flow (SWF) is a detached module of the spring Framework. This module is part of the Spring Web application development module stack, and the spring Web contains spring MVC.

The goal of Spring Web flow is to be the best solution for managing Web app page processes. SWF will be a powerful controller when your application requires complex navigation controls, such as wizards, to guide users through a series of steps in a larger transaction.

below we still from a simple Demo begin to understand it:

This example is a combination of Springmvc to implement the project structure:




Web . XML Configuration:

<?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" xmlns:web= "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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>CartApp</display-name><!--configuration springmvc required servlet--><  Servlet> <servlet-name>CartServlet</servlet-name> <servlet-class> Org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name> contextconfiglocation</param-name> <param-value>/web-inf/config/web-application-config.xml </ Param-value> </init-param> <load-on-startup>1</load-on-startup></servlet>< servlet-mapping><servlet-name>cartservlet</servlet-name><url-pattern>/spring/*</ Url-pattern></servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list&gt ;</web-app>

corresponding to the Spring MVC configuration file: Web-application-config.xml


<?xml version= "1.0" encoding= "Utf-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context" xsi:schemalocation= "Http://www.springframework.org/schema/beans     http://www.springframework.org/schema/ Beans/spring-beans-2.5.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/ Context/spring-context-2.5.xsd "><!--SPRINGMVC profile-June 14, 2015 15:45:54--><!--Search Samples.webflow Package @ Component annotations and deploy them to a container--><context:component-scan base-package= "Samples.webflow"/><!--enable annotation-based configuration-- <context:annotation-config/><import resource= "Webmvc-config.xml"/><import resource= " Webflow-config.xml "/></beans>

Two profiles introduced in it: Webmvc-config.xml


<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:     Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans.xsd "> <!--SPRINGMVC profile-June 14, 2015 15:45:54-- > <!--path resolution for the steering page. Prefix: prefix, suffix: suffix--<bean id= "Viewresolver" class= " Org.springframework.web.servlet.view.InternalResourceViewResolver "><property name=" Viewclass "value=" Org.springframework.web.servlet.view.JstlView "></property><property name=" prefix "value="/web-inf/  jsp/"></property><property name=" suffix "value=". jsp "></property></bean><!-- How to choose the right controller according to the HTTP request is a key feature in MVC, in spring MVC, the Handlermapping interface is the abstract-->< of this activity!-- simpleurlhandlermapping maps a URL to a controller--><bean id= "Viewmappings" class= through a configuration file Org.springframework.web.servlet.handler.SimpleUrlHandlerMapping "><!--/shopping.do request to be handled by Flowcontroller--><!--whether set to/shopping.do or/shopping, or/shopping.htm, The effect is the same, Flowcontroller will find the ID shopping flow to perform--><property name= "mappings" ><value>/shopping.do= Flowcontroller </value></property> <property name= "DefaultHandler" ><!-- Urlfilenameviewcontroller will map requests such as "/index" to "index" views--><bean class= " Org.springframework.web.servlet.mvc.UrlFilenameViewController "/></property> </bean><!--We just need to know The Flowcontroller can be executed based on the end of the client request and find the corresponding flow. Configure Flowcontroller only specify flowexecutor to--><bean id= "Flowcontroller" class= " Org.springframework.webflow.mvc.servlet.FlowController "><property name=" Flowexecutor "ref=" Flowexecutor "/ ></bean></beans>

for the Urlfilenameviewcontroller class, this is understood here because we are not accessing Controller to return the form of the page, then how can we access Web -inf under the protection of resources, it is through the role of this class to achieve. What is the deviation of the understanding here please give your valuable comments!

Webflow-config.xml:


<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:webflow= "http://www.springframework.org/schema/ Webflow-config "xsi:schemalocation=" Http://www.springframework.org/schema/beans http://www.springframework.org/ Schema/beans/spring-beans-2.5.xsd Http://www.springframework.org/schema/webflow-config/HTTP Www.springframework.org/schema/webflow-config/spring-webflow-config-2.0.xsd "> < profile of!--process-June 14, 2015 15    : 45:46-<!--Flowexecutor is a core interface of Spring Web flow, and it is the interface to start a flow. From a configuration standpoint, the default behavior of Spring Web Flow is sufficient-<!--Flowexecutor is to find the Flow to be executed by ID, as long as a flowexecutor is guaranteed.    As for this ID, it is to be specified by the user.    In the default configuration scenario, if the client sends the following URL request: Http://localhost:8080/CartApp/spring/shopping. From the point of view of Spring Web Flow, this URL indicates that the customer wants to execute a flow with the id "shopping", so it looks for flow named "shopping" in Flowregistry, by Flowex Ecutor is responsible for execution. --><webfLow:flow-executor id= "Flowexecutor"/><!--all flow definition files Its location is configured here, flow-builder-services to configure flow's features-- <!--Flowregistry is the warehouse where flow is stored, and each XML document defining flow is parsed, assigned a unique ID and stored in flowresigtry as a Flowdefinition object-->&l t;! --Each flow must have an ID to identify, if omitted in the configuration, then the flow default ID will be the definition file (XML file) file name minus the suffix of the string (for example, if you remove id= "shopping", Then the ID of flow is shopping.xml remove the suffix. Shopping as ID)--><!--the configuration of the Flow-builder-services property indicates in this Flow-registry "warehouse" Some of the basic features of flow in, for example, whether the data in Unified EL or OGNL, model objects need to be converted before they are displayed, etc.--><webflow:flow-registry id= "Flowregis Try "flow-builder-services=" flowbuilderservices "><webflow:flow-location path="/WEB-INF/flows/shopping.xml " Id= "Shopping"/><webflow:flow-location path= "/web-inf/flows/addtocart.xml" id= "AddToCart"/></webflow: flow-registry><!--The view in Web Flow renders--><webflow:flow-builder-services id= "through the view technology of the MVC framework" Flowbuilderservices "view-factory-creator=" Mvcviewfactorycreator "/><!--indicate the MVC framework's view REsolver, used to find resources--><bean id= "Mvcviewfactorycreator" class= "through the view name Org.springframework.webflow.mvc.builder.MvcViewFactoryCreator "><property name=" viewresolvers "ref=" Viewresolver "/></bean></beans>

Process file used: Shopping.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 "> <!-- The view in view-state corresponds to the JSP page in the JSP folder, on is the trigger event, to the state ID--><!--process file-June 14, 2015 15:51:56--><!-- Execute--><view-state id= "Viewcart" view= "Viewcart" ><transition on= "Submit" to= "ViewOrder" according to the order in the first place > </transition></view-state><view-state id= "ViewOrder" view= "ViewOrder" ><transition on= " Confirm "to=" orderconfirmed "></transition></view-state><view-state id=" orderConfirmed "view=" Orderconfirmed "><transition on=" Returntoindex "to=" Returntoindex "></transition></view-state ><end-state id= "Returntoindex" view= "externalredirect:servletrelative:/index.jsp" ></end-state> </flow>


The corresponding page:

index.jsp

<?xml version= "1.0" encoding= "Utf-8"? ><! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en"   "http://www.w3.org/TR/xhtml1/DTD/ Xhtml1-transitional.dtd "> 


viewcart.jsp


<body>


vieworder.jsp


<body>


orderconfirmed.jsp


<body>

These pages use the variable Flowexecutionurl, which represents the URL of flow when it executes to its current state. the value of Flowexecutionurl has been assigned by the code of the Spring Web Flow 2.0 framework and placed in the appropriate model for view access . The value of Flowexecutionurl contains the unique key that flow generates for each state during execution, so it cannot be obtained by other means. The value of _eventid in the request parameter corresponds to the value of the on property of the transition element in Shoppting.xml, and the corresponding transition is executed when the _eventid parameter is received.


Test how to use:

Access Address: http://localhost:8080/CartApp3/spring/index.jsp

In general, why should you configure so much content? The reasons are as follows:

How does Springweb Flow integrate with Spring Web MVC?

requests sent by the client are received first by the servlet container (Tomcat), and the Servlet container finds the appropriate application (Cartapp 3 ), and then find a servlet that conforms to the mapping criteria according to the configuration of Web. Xml. The servlet that processes the request in Spring Web MVC is Dispatcherservlet, and if the requested path satisfies the Dispatcherservlet mapping criteria, Dispatcherservlet will identify the spring IoC container All handlermapping, based on these handlermapping, match the best handler (typically a controller, that is, controllers) to process the request. When the controller is finished, it will generally return a view (view) name, Dispatcherservlet then find the corresponding view resource according to the view name to return to the client.

After figuring out the process of Spring web MVC processing requests, you can basically understand the configuration needed to integrate spring Web MVC with spring Web flow. In order for the client's request to become a request to perform a flow, the following issues are addressed:

  1. < Span style= "font-size:14pt;" lang= "ZH-CN" You need to configure handlermapping (or controller) handler style for handling flow requests in a -- configuration controller (Flowcontroller)
  2. the handler (or controller) is responsible for initiating the specified flow -- the Controller responsible for initiating Flow (Flowexecutor)
  3. The views involved during flow execution and after execution are presented to the client -- Configuration Resolution return view mode (viewresolvers)

All of these configurations are intended to be two: one is to turn the client's request into flow execution, and the other is to have the view returned to the client when the flow executes, or after execution has ended.

SOURCE download




Spring Web Flow Introduction Demo (a) attached source

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.