Spring Web Flow Introduction Demo (ii) and business integration of source code

Source: Internet
Author: User


First Part Demo just introduced a simple page jump, and then we want to implement the functions related to business logic.

The logic of the business involves the acquisition of data, transfer, preservation, related to the invocation of business function functions, such as the implementation of these functions can be done by Java code, but the definition of spring Web flow syntax is not related to Java, which requires spring Web flow with the Java generation The integration mechanism of code. The key to understanding this mechanism is to clarify two issues:

    • When is the business logic code called?
    • How is the data stored and passed by the business logic code after it is called?
when is the business logic code called?

In Spring Web flow, the execution of business logic code can be triggered by the following three scenarios:

    • The _eventid parameter is included in the client request
    • Execute a pointcut that is customized to the framework
    • Execute to <action-state> element

1, the _eventid parameter is included in the client request

This method is generally used in the transition between state, by specifying the value of the _eventid parameter, indicating the behavior of the customer, resulting in the occurrence of the corresponding event, in the Spring Web flow definition file can be evaluate element to specify the business logic to be processed

<transition on= "Submit" > <evaluate expression= "validator.validate ()"/> </transition>

when the client's request contains "_eventid=submit", the expression specified in the Evaluate element is executed, that is, the Validate method of the validator object is called.  

2, perform a custom entry to the framework

Springweb Flow defines 5 pointcuts that can be inserted into the relevant business logic code in these 5 pointcuts through the configuration of the flow definition file.

Springweb Flow Custom Entry point

before execution before the view actually render.

pointcut name

xml element name

trigger time

flow start

on-start

flow

state entry

on-entry

After entering a state, do something else before

view render

on-render

after entering the render flow of the view,

state exit

on-exit

before exiting state

Flow End

On-end

After flow execution is finished

On-render elements

<view-state id= "Viewcart" view= "Viewcart" ><on-render><evaluate expression= " Productservice.getproducts () "result=" Viewscope.products "/></on-render></view-state>

a follow-up will be described in detail, the following Demo even if you establish relationships with business logic in this way.

3, execute to <action-state> element

This <action-state> in Springweb Flow is the state that is designed to execute business logic. If the business logic code for an app is neither suitable for the client to trigger in transition, nor is it suitable for the Spring Web Flow custom pointcut, then consider adding the <action-state> element dedicated to the execution of that business logic.

action-state Example

<action-state id= "AddToCart" ><evaluate expression= "Cart.additem (productservice.getproduct (productId))"/ ><transition to= "productadded"/></action-state>

Follow-up will be described in detail in the next blog post.

How is the data stored and passed by the business logic code after it is called?

In the definition of Spring Web flow, you can directly use the expression language (expressions Language), the preceding code is used Unified EL, for the custom OGNL developers, can be changed by flow-builder-services configuration into use OGNL. Regardless of the expression language, Spring Web flow provides some fixed-name variables for saving and passing data.

In the spring Web flow solution, we know that the problem that spring Web flow is addressing is the data access scope, so spring Web flow provides two more important scopes, one for the Flow range and the other for Conversat Ion Range. With the two variables flowscope and conversationscope, Spring Web Flow provides a way to access data in both scopes.

<evaluate expression= "productservice.getproducts ()" result= "Flowscope.products"/>  

Note: Spring Web Flow 2.0 in the default configuration, the implementations of Flowscope and Conversationscope depend on Java serialization and deserialization techniques, so objects stored in Flowscope or Conversationscope The Java.io.Serializable interface needs to be implemented.

Note:

    1. Flow range. Objects within this range are created at the beginning of flow and are destroyed at the end of flow and are accessed through the "flowscope" variable name in the flow definition file.
    2. Conversation Range. Objects in this range are basically similar to the flow Range object, except that the flow of objects in the conversation range is accessible if other subflow are called, and the object can also be accessed in Subflow. (That is, Subflow can access objects in conversation)


Springweb Flow also provides a number of other variables to facilitate data access. If the Viewscope range is from entering the view-state to exiting the view-state end, Requestscope is no different from the general request range, and so on. There are also variables to get data outside of flow, such as Requestparameters, Messagecontext, and so on. A list of specific variables can be see the documentation that comes with Spring Web flow.

 


Demo Implementation:


Productservice class

Package Samples.webflow;import Java.util.arraylist;import Java.util.hashmap;import java.util.list;import Java.util.map;import Org.springframework.stereotype.Service; @Service ("Productservice") public class Productservice {Private Map<integer, product> products = new Hashmap<integer, product> ();p ublic Productservice () { Products.put (1, new product (1, "bulldog", +));p Roducts.put (2, new product (2, "Chihuahua",));p Roducts.put (3, new Product (3, "Labrador", 2000));} Public list<product> getproducts () {return new arraylist<product> (Products.values ());} Public Product getproduct (int productId) {return products.get (productId);}}

Service annotations indicate that the spring IOC container Initializes a bean named Productservice, which can be accessed directly from the definition of spring Web flow. (This is why you added annotations in web-application-config.xml)

Modify the Shopping.xml file

To display the item in the Viewcart page, simply call the Productservice GetProducts method in the On-render pointcut of the view-state element and save the resulting result in viewscope.

Modified 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 corresponding state ID--><view-state id= "viewcart" view= "Viewcart" >< on-render><!--to display the item in the Viewcart page, simply call Productservice's GetProducts method in the On-render pointcut of the view-state element and save the resulting result to the view The results of the GetProducts method in Scope--<!--productservice--are stored in variables named products in Viewscope, and the Code of the JSP page accesses the variable directly. -<!--Specify the business logic to be processed via the Evaluate element--><evaluate expression= "productservice.getproducts ()" result= " Viewscope.products "/></on-render><transition on=" Submit "to=" ViewOrder "></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>

Modify Viewcart.jsp Page

The result of Productservice's GetProducts method is stored in a variable named products in Viewscope, which can be accessed directly by the code of the JSP page.  

Modified viewcart.jsp Page

<?xml version= "1.0" encoding= "Utf-8"? ><%@ taglib prefix= "C" uri= "Http://java.sun.com/jsp/jstl/core"%> <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">


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


View cart page:



SOURCE download

Summary:

The above code implementation is combined with the first post-blog integration of business logic to operate, the main introduction of the business and Spring Web flow combination, the next blog will be introduced in succession of the process of nesting.




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Spring Web Flow Introduction Demo (ii) and business integration of source code

Related Article

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.