springwebflow簡單部署

1. 配置相關文件
  • 在 /WEB-INF/lib 目錄下導入相關類庫
  • 在 webmvc-config.xml 中添加與 Spring Web Flow 集成的配置
  • 添加 Spring Web Flow 的配置文件 webflow-config.xml
  • 添加 flow 定義文件 shopping.xml
  • 添加三個 jsp 頁面

2.web.xml 配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/config/web-applocation-config.xml</param-value>
       </init-param>
       
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/spring/*</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>redirect.jsp</welcome-file>
    </welcome-file-list>
</web-app>
3.applicationContext.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:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
       ">  
       
       
</beans>
4.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"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
      http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
	http://www.springframework.org/schema/context 
	http://www.springframework.org/schema/context/spring-context-2.5.xsd">
	<!-- 搜索 samples.webflow 包裏的 @Component 註解,並將其部署到容器中 --> 
	<context:component-scan base-package="samples.webflow" /> 
	<!-- 啓用基於註解的配置 --> 
	<context:annotation-config /> 
         <!-- 啓用mvc註解的配置 --> 
          <mvc:annotation-driven/>
	<import resource="webmvc-config.xml"/>   
        <import resource="webflow-config.xml"/>
</beans>
5.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-2.5.xsd
">
    <bean id="viewResolver" 
              class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
        <property name="viewClass" 
                    value="org.springframework.web.servlet.view.JstlView"/> 
        <property name="prefix" value="/WEB-INF/jsp/"/> 
        <property name="suffix" value=".jsp"/> 
    </bean> 
        
    <bean id="viewMappings" 
              class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> 
        <property name="defaultHandler">
	  <!-- UrlFilenameViewController 會將 "/index" 這樣的請求映射成名爲 "index" 的視圖 --> 
            <bean class="org.springframework.web.servlet.mvc.UrlFilenameViewController" /> 
        </property>  
        <property name="mappings">
            <value>
                /shopping.htm=flowController
            </value>
        </property>
    </bean>
    
    <bean id="flowController" 
        class="org.springframework.webflow.mvc.servlet.FlowController"> 
        <property name="flowExecutor" ref="flowExecutor"/> 
    </bean>             
</beans>
6.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"
       xmlns:faces="http://www.springframework.org/schema/faces"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/faces http://www.springframework.org/schema/faces/spring-faces-2.0.xsd
       http://www.springframework.org/schema/webflow-config http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.0.xsd ">



<!--存放flowDefine 的倉庫-->
<webflow:flow-registry id="flowRegistry" flow-builder-services="flowBuilderServices">
<webflow:flow-location path="/WEB-INF/flows/shopping.xml" id="shopping"/>
</webflow:flow-registry>   

<!--通過此接口來啓動 flow-->
<webflow:flow-executor id="flowExecutor" />

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


<!--flow-builder-services 指明瞭 flow-registry “倉庫”裏的 flow 的一些基本特性 -->
<webflow:flow-builder-services id="flowBuilderServices" view-factory-creator="mvcViewFactoryCreator"/>

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


</beans>

7.shopping.xml  flow的定義文件

<?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">
     
     
<view-state id="viewCart" view="viewCart">
<!--<on-render> 
<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>
8.   4個jsp文件

index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

  <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Welcome to Spring Web MVC project</title>
    </head>
    <body>
    <h1>Hello!</h1><br/>
<a href="shopping.htm">View Cart</a>
    </body>
 </html>



orderConfirmed.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
    <h1>Order Confirmed</h1>
    <a href="${flowExecutionUrl}&_eventId=returnToIndex">Return to index</a>
    </body>
</html>


viewCart.jsp

 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>View Cart</title> 
</head> 
<body> 
<h1>View Cart</h1>
<a href="${flowExecutionUrl}&_eventId=submit">Submit</a>
</body> 
</html>



viewOrder.jsp

 

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
    <h1>Order</h1>
   <a href="${flowExecutionUrl}&_eventId=confirm">Confirm</a>
    </body>
</html>







發佈了39 篇原創文章 · 獲贊 1 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章