Board logo

标题: Spring Web Flow 2.0 入门(3) [打印本页]

作者: look_w    时间: 2018-8-5 12:52     标题: Spring Web Flow 2.0 入门(3)

配置 Spring Web MVCSpring Web Flow 2.0 就是 Spring Web MVC 的一个扩展,如果粗略一些来讲,所谓 flow 就相当于 Spring Web MVC 中一种特殊的 controller ,这种 controller 可通过 XML 文件加以配置,因此在使用 Spring Web Flow 2.0 前须先对 Spring Web MVC进行配置,步骤如下:
创建 Web 应用的目录结构本示例应用将采用 eclipse Dynamic Web Project 向导默认生成的目录结构,在 WEB-INF 目录下添加 config 和 flows 子目录,其中 config 子目录用来存放各种配置文件, flows 子目录下存放 Spring Web Flow 的定义文件。最后目录如图3所示:
图 2 目录结构在 /WEB-INF/lib 下导入相关类库只需将以下几个 jar 包导入 /WEB-INF/lib 目录下就可以了:
声明 DispatcherServlet 并指定配置文件为使用 Spring Web MVC ,须在 web.xml 中声明 DispatcherServlet ,见清单3:
清单 3 声明 DispatcherServlet 和指定配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
<servlet>
  <servlet-name>CartServlet</servlet-name>
  <servlet-class>
  org.springframework.web.servlet.DispatcherServlet
  </servlet-class>
  <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
    /WEB-INF/config/web-application-config.xml
    </param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>




添加 DispatcherServlet 映射要让 DispatcherServlet 处理所有以 /spring/ 开头的请求,见清单 4:
清单 4 web.xml 中的 DispatcherServlet映射
1
2
3
4
<servlet-mapping>
<servlet-name>CartServlet</servlet-name>
<url-pattern>/spring/*</url-pattern>
</servlet-mapping>




创建 web-application-config.xml开发基于 Spring Web Flow 的应用往往会有大量的配置,这些配置全放在一个文件中是不合适的。本示例参考 Spring Web Flow 2.0 自带示例,将不同功能的配置文件分开。其中 web-application-config.xml 用于配置与 Web 应用全局相关的内容, Spring Web MVC 的相关配置放在 webmvc-config.xml 中,教程后面要添加的 Spring Web Flow 的配置则放在 webflow-config.xml 中。在 web-application-config.xml 中用 import 元素导入其他的配置文件。 web-application-config.xml的内容见清单5:
清单 5 web-application-config.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?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"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.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 />
    <import resource="webmvc-config.xml"/>
</beans>




加入注解功能是出于最后运行 Web Flow 示例的需要,在这里只要知道注解功能已被启用就可以了。




欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) Powered by Discuz! 7.0.0