Welcome!

Java IoT Authors: Pat Romanski, Elizabeth White, Zakia Bouachraoui, William Schmarzo, Yeshim Deniz

Related Topics: Java IoT

Java IoT: Article

The Promise of Handling Complex Page Navigations in Any Web Application

The promise of handling complex page navigations in any Web application

Page navigation requirements become more demanding as Web applications get bigger and more complex. Hard-coded page flow rules make applications less resilient to changes. In this scenario, reusing business logic is one aspect and reusing page flow becomes another aspect. Especially in situations that demand a wizard-kind behavior, it's essential to capture application page flow logic in a declarative fashion.

Spring Web Flow (SWF) is a powerful framework for implementing page flows in a Web application. Even though SWF is part of the Spring Web application development suite that includes Spring MVC, its flexible architecture allows it to be used with any presentation tier Web framework including Apache Struts and JSF. SWF is now available as a development-ready PR 3.0 version and is expected to be part of Spring 1.3.

In this article, we'll explore the features of SWF and use it to build a sample page flow scenario.

Spring Web Flow in Action
In SWF, every flow is a Finite State Machine (FSM). It consists of states and transitions. A state represents the execution of some actions (business logic) or a view that's displayed to the user for user input.

When the FSM starts, the flow enters the start state (a state that's specially marked as start state). A state can be an action state or a view state (SWF has other state types too like decision state, sub-flow state, and end state). Both action states and view states can define one or more transitions from them to a target state. An event triggers a transition from one state and moves the flow to the new state.

When an action state is entered, the flow executes one or more configured action objects for that state. The execution of an action can produce a logical result in the form of an event and it's processed to select a suitable transition. This transition is applied (executed) and as a result the flow enters the next state as defined by this transition.

When a view state is entered, the flow is temporarily paused and a view, as configured for that state, is displayed to the user. When the flow receives an event (user input can be encapsulated as an event) from the user, it resumes execution and processes the event to transition to the next state.

This new state is then processed in the same way and so on. The flow continues to execute until it reaches the end state (specially marked as end state) where the flow is terminated and a view (as configured for the end state) is displayed to the user. Figure 1 depicts how states are processed in FSM.

Flow Builders
SWF can build a flow definition consisting of states and transitions from an XML configuration file. Since SWF implements the classic GoF Builder design pattern, it provides the flexibility to define and use custom flow building logic. Figure 2 shows the flow builder class diagram.

SWF has many FlowBuilder implementations. The XMLFlowBuilder is one of them that can build a flow from an XML configuration file.

The FlowFactoryBean class internally uses a FlowBuilder implementation and acts as an assembler for creating a Flow.

FlowBuilder builder = ...
Flow flow = new FlowFactoryBean(builder).getFlow();

The XMLFlowFactoryBean implementation uses the XMLFlowBuilder to create a flow. In a Spring container, you can configure an XMLFlowFactoryBean as shown below and use it to build a flow:

<bean id="aFlow" class="org.springframework.web.flow.config.XMLFlowFactoryBean">
   <property name="location" value="classpath:myflow.xml" />
</bean>
BeanFactory factory = ....
Flow flow = (Flow)factory.getBean("aFlow");

[Spring recognizes the defined bean as a 'factory bean' and invokes its getObject() method, which in turn invokes the getFlow() method to create and return a flow.]

You can also create a flow by sub-classing the AbstractFlowBuilder class as shown below. This class defines many helper methods like addViewState() and addActionState() to define the flow at runtime.

public class AFlowBuilder extends AbstractFlowBuilder {
   protected String flowId() {return "aFlow";}
   public void buildStates() {
    addViewState("stateId","viewName",transition);
    addActionState("stateId", targetAction, transition);
    ...
   }
}

A flow builder internally uses a FlowServiceLocator to create all flow elements including a Flow, a State, and a Transition. The BeanFactoryFlowServiceLocator implementation uses Spring's bean factory to look up and create flow elements. By default the XMLFlowBuilder uses the BeanFactoryFlowServiceLocator.

The XMLFlowBuilder allows a flow element (flow/state /transition, etc.) to have the common optional properties bean, classref, class, and autowire. If a 'bean' or 'classref' property is provided the XMLFlowBuilder uses this value for resolving the bean reference using Spring. Otherwise, if a "class" property is given, a new element of the given type is created using reflection. The 'autowire' property denotes whether to use Spring's autowire (the ability to automatically apply dependency injection) capability or not.

A Sample Scenario - Shopping Cart Checkout
Let's discuss a simple scenario where a user uses a Web wizard to check out items in his shopping cart that he wants to buy. Figure 3 shows this scenario.

Step 1: Create an XML Web flow definition (checkoutFlow.xml) as shown in Listing 1.

Step 2: Create an XML flow factory bean entry in Spring's context XML configuration file as shown in Listing 2.

Step 3: Execute the flow. See Listing 3.

More Stories By Kishore Kumar

Kishore Kumar works as a Java architect at U.S. Technology (www.ustri.com). He specializes in J2EE applications.

Comments (5)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


IoT & Smart Cities Stories
Tapping into blockchain revolution early enough translates into a substantial business competitiveness advantage. Codete comprehensively develops custom, blockchain-based business solutions, founded on the most advanced cryptographic innovations, and striking a balance point between complexity of the technologies used in quickly-changing stack building, business impact, and cost-effectiveness. Codete researches and provides business consultancy in the field of single most thrilling innovative te...
Codete accelerates their clients growth through technological expertise and experience. Codite team works with organizations to meet the challenges that digitalization presents. Their clients include digital start-ups as well as established enterprises in the IT industry. To stay competitive in a highly innovative IT industry, strong R&D departments and bold spin-off initiatives is a must. Codete Data Science and Software Architects teams help corporate clients to stay up to date with the mod...
In his session at 21st Cloud Expo, Raju Shreewastava, founder of Big Data Trunk, provided a fun and simple way to introduce Machine Leaning to anyone and everyone. He solved a machine learning problem and demonstrated an easy way to be able to do machine learning without even coding. Raju Shreewastava is the founder of Big Data Trunk (www.BigDataTrunk.com), a Big Data Training and consulting firm with offices in the United States. He previously led the data warehouse/business intelligence and Bi...
The Japan External Trade Organization (JETRO) is a non-profit organization that provides business support services to companies expanding to Japan. With the support of JETRO's dedicated staff, clients can incorporate their business; receive visa, immigration, and HR support; find dedicated office space; identify local government subsidies; get tailored market studies; and more.
With the introduction of IoT and Smart Living in every aspect of our lives, one question has become relevant: What are the security implications? To answer this, first we have to look and explore the security models of the technologies that IoT is founded upon. In his session at @ThingsExpo, Nevi Kaja, a Research Engineer at Ford Motor Company, discussed some of the security challenges of the IoT infrastructure and related how these aspects impact Smart Living. The material was delivered interac...
CloudEXPO has been the M&A capital for Cloud companies for more than a decade with memorable acquisition news stories which came out of CloudEXPO expo floor. DevOpsSUMMIT New York faculty member Greg Bledsoe shared his views on IBM's Red Hat acquisition live from NASDAQ floor. Acquisition news was announced during CloudEXPO New York which took place November 12-13, 2019 in New York City.
Atmosera delivers modern cloud services that maximize the advantages of cloud-based infrastructures. Offering private, hybrid, and public cloud solutions, Atmosera works closely with customers to engineer, deploy, and operate cloud architectures with advanced services that deliver strategic business outcomes. Atmosera's expertise simplifies the process of cloud transformation and our 20+ years of experience managing complex IT environments provides our customers with the confidence and trust tha...
Intel is an American multinational corporation and technology company headquartered in Santa Clara, California, in the Silicon Valley. It is the world's second largest and second highest valued semiconductor chip maker based on revenue after being overtaken by Samsung, and is the inventor of the x86 series of microprocessors, the processors found in most personal computers (PCs). Intel supplies processors for computer system manufacturers such as Apple, Lenovo, HP, and Dell. Intel also manufactu...
At CloudEXPO Silicon Valley, June 24-26, 2019, Digital Transformation (DX) is a major focus with expanded DevOpsSUMMIT and FinTechEXPO programs within the DXWorldEXPO agenda. Successful transformation requires a laser focus on being data-driven and on using all the tools available that enable transformation if they plan to survive over the long term. A total of 88% of Fortune 500 companies from a generation ago are now out of business. Only 12% still survive. Similar percentages are found throug...
Darktrace is the world's leading AI company for cyber security. Created by mathematicians from the University of Cambridge, Darktrace's Enterprise Immune System is the first non-consumer application of machine learning to work at scale, across all network types, from physical, virtualized, and cloud, through to IoT and industrial control systems. Installed as a self-configuring cyber defense platform, Darktrace continuously learns what is ‘normal' for all devices and users, updating its understa...