Monday, July 18, 2011

Webflow - Step 1.Adding webflow to existing project

The following example takes you through the configurations to enable web flow in an existing Spring based web project. 

ApplicationContext.xml

ApplicationContext.xml is the main configuration that has the MVC based configurations and we are including webflow configurations to it.

<?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"
    xmlns:lang="http://www.springframework.org/schema/lang" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:webflow-config="http://www.springframework.org/schema/webflow-config"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.0.xsd
        http://www.springframework.org/schema/webflow-config http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.0.xsd">


    <!-- View Resolvers -->
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <!--
        Register SimpleControllerHandlerAdapter to handle non-webflow URLs as
        well. Without this configuration, only the web flow URLs will be
        handled by the DispatcherServlet
    -->
    <bean
        class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter">
    </bean>

    <!-- URL Handlers -->
    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <value>
                /hello.do=helloController
                /flow.do=flowController
            </value>
        </property>
        <property name="order" value="1"></property>
    </bean>
    <bean id="helloController" class="org.mybusiness.webflowtut.controllers.HelloController" />

    <!-- Importing WebFlow Configurations -->
    <import resource="./WebFlowContext.xml" />
</beans>

WebFlowContext.xml

WebFlowContext.xml contains the webflow specific configurations to register webflows to your project.

<?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"
    xmlns:lang="http://www.springframework.org/schema/lang" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:webflow-config="http://www.springframework.org/schema/webflow-config"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.0.xsd
        http://www.springframework.org/schema/webflow-config http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.0.xsd">

    <!-- Web Flow Configurations -->

    <!-- Register your flows in a flow registry -->
    <webflow-config:flow-registry id="flowRegistry"
        flow-builder-services="flowBuilderServices">
        <webflow-config:flow-location-pattern
            value="/WEB-INF/conf/flows/**/*-Flow.xml" />
    </webflow-config:flow-registry>

    <!-- Deploy a flow executor to execute the flows -->
    <webflow-config:flow-executor id="flowExecutor"
        flow-registry="flowRegistry">
    </webflow-config:flow-executor>

    <!--
        Flow builder services can be used to customize the flow builds in a
        flow registry. In this case, we are integrating the web flow with MVC
        view resolvers
    -->
    <webflow-config:flow-builder-services
        id="flowBuilderServices" view-factory-creator="mvcViewFactoryCreator" />

    <bean id="mvcViewFactoryCreator"
        class="org.springframework.webflow.mvc.builder.MvcViewFactoryCreator">
        <property name="viewResolvers" ref="viewResolver"></property>
    </bean>

    <!-- Enable flow handling for incoming URL requests -->
    <bean id="flowHandlerAdapter"
        class="org.springframework.webflow.mvc.servlet.FlowHandlerAdapter">
        <property name="flowExecutor" ref="flowExecutor"></property>
    </bean>

    <!--
        FlowController is used to handle the requests that has to be resolved
        to flows instead of MVC controllers
    -->
    <bean id="flowController" class="org.springframework.webflow.mvc.servlet.FlowController">
        <property name="flowExecutor" ref="flowExecutor"></property>
        <property name="flowUrlHandler">
            <bean
                class="org.springframework.webflow.context.servlet.WebFlow1FlowUrlHandler" />
        </property>
    </bean>

    <!-- Define beans Supporting User Creation -->
    <bean id="userService" class="org.mybusiness.webflowtut.services.UserService"></bean>

</beans>

With these changes added to your project, webflow is enabled. Now you can start creating flows and add them to your project under /WEB-INF/conf/flows/<anyfoldername>/*-Flow.xml

Proceed to Step 2: Creating Flow.xml and View files

No comments:

Post a Comment