Spring Web Flow yes Spring Subprojects of the framework , The function is to make the program run according to the specified process .

1 Installation configuration Spring Web Flow

although Spring Web Flow yes Spring Subprojects of the framework , But it's not Spring Part of the frame , So before building process based applications , You need to add Spring Web Flow The required jar package . Download at :http://www.springframework.org/webflow

Spring Web Flow It's about building and Spring MVC Based on . This means that all process requests need to go through Spring MVC Of DispatcherServlet. So we need to be able to Spring In the context of the application .

1.1 FlowRegistry

FlowRegistry It's for storage. flow The warehouse of , Each definition flow Of XML After the document is parsed , Will be assigned a unique id , And FlowDefinition The form of the object is stored in FlowResigtry in .

<webflow:flow-registry id="flowRegistry">
<webflow:flow-location path="/WEB-INF/flows/shopping.xml" id=”shopping”/>
</webflow:flow-registry>

Every flow Must have id To mark , If omitted in configuration , Then the flow default id Will be the definition file (xml file ) Remove the suffix from the file name of ( For example, in this case, if you remove id="shopping", that flow Of id Namely shopping.xml Remove the suffix .xml After shopping As id).

1.2 FlowExecutor

FlowExecutor yes Spring Web Flow A core interface of , Start something flow , It's all through this interface . From a configuration point of view , Just make sure there's a FlowExecutor That's all right. , Spring Web Flow The default behavior of is enough .

<webflow:flow-executor id="flowExecutor" />

1.3 Spring Web Flow How to communicate with Spring Web MVC Integrated ?

Request sent by client , First of all, I'll let you know servlet Containers ( In this tutorial example is Tomcat ) receive , servlet The container will find the corresponding application ( In this tutorial is CartApp ), According to web.xml The configuration of is found to meet the mapping conditions servlet To deal with it . Spring Web MVC To handle requests in servlet yes DispatcherServlet , If the requested path satisfies DispatcherServlet The mapping condition of , be DispatcherServlet Will find out Spring IoC Everything in the container HandlerMapping , According to these HandlerMapping The best match of all handler ( In general, it's controller , I.e. controller ) To process requests . When Controller Finished processing , It usually returns a view ( View ) Name ,DispatcherServlet According to this view Find the corresponding view resource and return it to the client .

Make clear Spring Web MVC After the process of processing the request , Basically, we can understand the need to integrate Spring Web MVC And Spring Web Flow The configuration needed . In order to make the client's request become the execution of a flow Request , To solve the following problems :

  1. It needs to be somewhere HandlerMapping Configuration is responsible for processing flow Requested handler ( or controller )
  2. The handler ( or controller ) Be responsible for starting the specified flow
  3. flow The views involved during and after execution should be presented to the client

1.4 FlowHandler and FlowController

Now? , A receiving execution is needed flow Request , And then start the corresponding flow Of handler ( processor ), Spring Web Flow 2.0 Two options are offered . The first solution is to write and implement FlowHandler The class of the interface , Let this class implement this function . The second option is to use a ready-made FlowController The controller . The first is more flexible , In many cases, it may be the only choice , But for each flow We need to write the corresponding FlowHandler . The examples in this tutorial take the second approach , Yes FlowHandler Please refer to Spring Web Flow 2.0 The documentation that comes with you . FlowController It's actually an adapter , In general , We just have to understand FlowController Depending on the end of the client request , Find out the corresponding flow To execute . To configure FlowController Just specify FlowExecutor that will do .

<bean id="flowController" class="org.springframework.webflow.mvc.servlet.FlowController">
<property name="flowExecutor" ref="flowExecutor"/>
</bean>

In addition, it needs to be done in HandlerMapping Named in the /shopping.do Request by the flowController To deal with it :

<bean id="viewMappings"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<!-- /shopping.do Request by the flowController To deal with it -->
<property name="mappings">
<value> /shopping.do=flowController </value>
</property>
<span style="white-space:pre"> </span>......
</bean>
It's important to point out that , Regardless of setting /shopping.do Or set it to /shopping , perhaps /shopping.htm , The effect is the same , flowController They'll find it id by shopping Of flow To execute .

1.5 FlowBuilder Services

flow-builder-services Property configuration indicates that in this flow-registry “ Warehouse ” Inside flow Some basic characteristics of , for example , Yes, it is Unified EL still OGNL 、 model ( Model ) Whether the data in the object needs to be converted before being displayed , wait . In this example , We need to be in flow-builder-services Property indicates Spring Web Flow What's used in view , from Spring Web MVC Of “ View Resolver ” Search for , from Spring Web MVC Of “ View Class” Parsing , Finally, it is presented to the user .

<!-- FlowRegistry It's for storage. flow The warehouse of , Each definition flow Of XML After the document is parsed , Will be assigned a unique id , And FlowDefinition The form of the object is stored in FlowRes igtry in -->
<!-- all flow Its location is configured here , flow-builder-services Used for configuration flow Characteristics of -->
<webflow:flow-registry id="flowRegistry" 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>

 <!--Web Flow The view in the MVC Frame view technology to present -->
<webflow:flow-builder-services id="flowBuilderServices" view-factory-creator="mvcViewFactoryCreator" />
<!-- To specify MVC Framework of the view resolver , Used by view Find resources by name -->
<bean id="mvcViewFactoryCreator" class="org.springframework.webflow.mvc.builder.MvcViewFactoryCreator">
<property name="viewResolvers" ref="viewResolver" />
</bean>

All of these configurations are for two purposes : One is to turn the client's request into flow Implementation , Second, let flow In the process of execution 、 Or the view obtained after execution can be returned to the client .

2 Spring Web Flow The components of

Spring Web Flow in , The process is made up of 3 It's defined by two main elements : state 、 Transfer and process data .

2.1 state

Spring Web Flow Defined 5 There are two different types of States (state)—— Behavior 、 Decision making 、 end 、 Subprocesses and view States .

In addition to the end state , Other states can be changed to other states , Generally by defining transitions in state (transition) To achieve the transition to other states , The occurrence of transformation is generally determined by events (event) To trigger .

2.1.1 View state

View state is used to present information for users and to participate in the process . The actual view can be of any type , But usually with JSP To achieve .

In the process XML In file , Use <view-state> Define view state .

<view-state id="viewCart" view="viewCart">
<transition on="submit" to="viewOrder">
</transition>
</view-state>

2.1.2 State of conduct

The behavior state is that the program itself is performing a task , In general, it will trigger Spring Managed Bean And transfer to another state according to the return result of the method .

In the process XML In file , Use <action-state> Define behavior states .

<action-state id="addToCart">
<evaluate expression="cart.addItem(productService.getProduct(productId))" />
<transition to="productAdded" />
</action-state>

<action-state> There's usually one of them <evaluate> Elements ,<evaluate> The element specifies what the behavior state does .

2.1.3 Decision state

The decision state can produce two branches when the process is executed . Judge a Boolean Expression of type , Then choose one of the two states .

In the process XML In file , Use <decision-state> Define the decision state .

<decision-state id="ticketGrantingTicketExistsCheck">
<if test="flowScope.ticketGrantingTicketId neq null" then="hasServiceCheck" else="gatewayRequestCheck" />
</decision-state>

2.1.4 Sub process status

The subprocess state is used to invoke another process in a running process .

In the process XML In file , Use <subflow-state> Define sub process state .

<subflow-state id="addProductToCart" subflow="addToCart">
<transition on="productAdded" to="viewCart" />
</subflow-state>

2.1.5 End state

The end state is used to end a process .

In the process XML In file , Use <end-state> Define the end state .

<end-state id="returnToIndex" view="externalRedirect:servletRelative:/index.jsp">
</end-state>

If you end up with a subprocess , Then the process will start from the parent process <subflow-state> Continue to execute , Sub process <end-state> Of ID Will trigger the parent process <subflow-state> Of transition.

If <end-state> Set up view attribute , The specified view will be rendered , Add “externalRedirect:” The prefix will be redirected to a page outside the process , And add “flowRedirect:” Redirect to another process .

If the ending process is neither a subprocess nor specified view attribute , Then the process will just end , The browser will load the basics of the process URL Address , Start a new process instance .

2.2 Transfer

In addition to the end state in the process , Each of the remaining states needs to have at least one transition , Only in this way can we know where the process will go once this state is completed . States can also have multiple transitions .

Transfer use <transition> Elements to define , It will be used as various state elements (<action-state> etc. ) Child elements .

<transition on="addToCart" to="addProductToCart" />

attribute to Used to specify the next state of the process .

attribute on Used to specify the event that triggers the transition .( One more on-exception Properties are not given in this example , It is used to specify the exception that triggers the transition )

When multiple states use the same transition , You can set up a global transition to simplify the code .

<global-transitions>
<transition on="cancelShopping" to="returnToIndex"/>
</global-transitions>

After defining the global transfer , All States in the process will have this by default cancelShopping Transfer .

2.3 Process data

The process state may need to “ Piggyback ” Some of the data , These data are process data , Process data is stored in variables , Variables can be created in many forms , And you can set the scope .

2.3.1 Declare variables

The simplest form of creating variables in a process is to use <var> Elements :

<var name="mycart" class="samples.webflow.Cart" />

The code above creates a Cart Instance and put it in a place called mycart Variables in .

Use <evaluate> perhaps <set> Elements can also create variables :

<evaluate result="conversationScope.cart" expression="mycart"/>
<set name="conversationScope.cart" value="mycart"/>

<evaluate> Elements and <set> The elements are very similar , It's all the result of setting the variable to the expression evaluation . Here's the variable cart The scope of is conversationScope, Next, let's talk about the scope of process data .

2.3.2 Define the scope of process data

Spring Web Flow Defined 5 Species scope , The following table :

Range Scope and visibility
Conversation Create... At the beginning of the highest level process , Destroy at the end of the highest level process . Shared by the highest level process and all its subprocesses
Flow Create... When the process starts , Destroy at the end of the process . Only visible in the process of creating it
Request Create... When a request enters the process , Destroy on process return
Flash Create... When the process starts , Destroy at the end of the process . After rendering in view state , It will also be removed
View Create... When entering view state , Destroy when this state exits . Visible only in view state

When using <var> When an element declares a variable , The variable is always Flow Scoped , That is to say Flow Define variables in scope . When using <set> or <evaluate> when , Scope through name or result The prefix of the property specifies .

Reference resources :

http://www.cnblogs.com/xwdreamer/archive/2011/11/10/2296939.html

http://blog.csdn.net/hejingyuan6/article/details/46508821

Reprint please indicate the source

Spring More articles on process

  1. 【Spring Source code analysis 】 Non lazy loading singleton Bean Some operations before and after initialization

    Preface The last two articles [Spring Source code analysis ] Non lazy loading singleton Bean Initialization process ( Part 1 ) and [Spring Source code analysis ] Non lazy loading singleton Bean Initialization process ( The next part ) A single example of non lazy loading is analyzed in detail Bean The initialization process , ...

  2. Spring Source code analysis : Non lazy loading singleton Bean Some operations before and after initialization

    The last two articles Spring Source code analysis : Non lazy loading singleton Bean Initialization process ( On ) and Spring Source code analysis : Non lazy loading singleton Bean Initialization process ( Next ) A single example of non lazy loading is analyzed in detail Bean The initialization process , The whole process starts with A ...

  3. How to see Spring Source code

    Want to know more about Spring Source code , I think the first step is to have a very simple framework that can run , Now I'll teach you to build the simplest Spring frame , And it's based on Java Config Zero configuration of form Spring frame . The first ...

  4. Cao Gong said Spring Boot Source code (18)-- Spring AOP Source code analysis trilogy , It's almost over at last (aop:config Complete parsing 【 Next 】)

    Let me write it out front Relevant background and resources : Cao Gong said Spring Boot Source code (1)-- Bean Definition What is it , attach spring Mind map sharing Cao Gong said Spring Boot Source code (2)-- Bean ...

  5. Spring establish Bean The process of Debug

    Catalog Spring technological process Debug 1.1 Spring Test environment construction 1.2 Debug Container creation process 1.3 AbstractApplicationContext Of refresh() Contains 13 Two methods of analysis ...

  6. SSH Three frame knowledge problems

    Struts Talk to you about Struts The understanding of the . answer : 1.struts It's a press MVC Pattern design Web Layer framework , In fact, it is a big servlet, This Servlet be known as ActionServlet, or Act ...

  7. SSH frame (2)

    Personal classification : Java interview   Struts Talk to you about Struts The understanding of the . answer : 1.struts It's a press MVC Pattern design Web Layer framework , In fact, it is a big servlet, This Servlet be known as Acti ...

  8. SSH(Struts2+Spring+Hibernate) Framework building process

    Add support I'd like to introduce you first MyEclipse9 The built-in framework supports the building process :( Complete steps Stupid learning ..~) First of all, let's build one Web project : One .Hibernate( The data layer ) Build : Related description Ⅰ. Server and number ...

  9. spring Automatic scanning 、DispatcherServlet Initialization flow 、spring controller Controller Process analysis

    spring Automatic scanning 1. Auto scan parser ComponentScanBeanDefinitionParser, from doScan Start to scan and parse the class annotation information under the specified package path and register it in the factory container . 2. After entering findCa ...

Random recommendation

  1. redis Learning notes (7)-cluster client (jedis) Code example

    Last time I learned cluster The construction and redis-cli How to operate under the terminal , But a more common scenario is in the program code to cluster Reading and writing , This needs to be redis-client Yes cluster Mode support , at present spring-dat ...

  2. VB in NumericUpDown Control How to set trigger events for manual input

    Private Sub numDuration_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) ...

  3. OpenCV And edge detection

    Last one <OpenCV And Image smoothing > in , The image smoothing mentioned , From the point of view of signal processing , It's actually a kind of “ low pass filter ”. In this , The edge of a digital image , Because it's usually an area where pixel values change dramatically (“ high frequency ”), So ...

  4. The foundation of programming languages —— Get it done JavaIO

    keyword :IO Basics ,JUnit Life cycle , Byte stream , Character stream , Character encoding , Object flow , serialize , Deserialization Java I/O Flow is a set of sequential , Set of bytes with start and end points . It is the general name and abstract of data transmission between device files . stay IO It's about ...

  5. SqlServer xml type Query and operation

        2.xpath:    Addressing language , similar windows Directory search ( Never used dir Go to the wall if you want )                                    Grammar format , These grammars can be combined into ...

  6. JS Imitate Taobao's left menu

    http://www.webdm.cn/webcode/1c724a06-06f4-4c4f-931a-c683285fa700.html

  7. Makefile Arrangement

    Makefile Inside subst Usage is $(subst FROM,TO,TEXT), the TEXT What's in it comes from FROM Turn into TO Makefile String handling functions in Format : $(subst <from& ...

  8. 【WPF】GridControl Use

    One . Preface stay WPF We often use DataGrid To display data , stay Dev There is a similar control in —GridControl. Two . Example <dxg:GridControl Grid.Row=" I ...

  9. js A little bit of code

    Load order .1 Wait for the page to load <script type="text/javascript"> jQuery(window).load(function(){ ... } ...

  10. solr the second day Jingdong case Course documentation Useful

    Full text retrieval technology   Lucene&Solr               Part3 1.  Course plan 1. Solr Configure Chinese Analyzer a) Schema.xml Configuration of b)  To configure IKAnalyzer ...