首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

Spring Web Flow 2.0 入门(4)

Spring Web Flow 2.0 入门(4)

创建 webmvc-config.xmlwebmvc-config.xml 主要用于配置 Spring Web MVC 。所要做的就是添加一个 view resolver (视图解析器),用于将视图名解析成真实的视图资源。另外,再配置好 URL 请求的 handler (处理器),用于将 URL 请求定向到某个控制器,在本例中,用到的是 UrlFilenameViewController。
清单 6 webmvc-config.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?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.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>
  </bean>
</beans>




创建 index.jsp现在的 index.jsp 只是显示一行文字。
清单 7 index.jsp
1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="utf-8" ?>
<!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>Cart Application</title>
  </head>
  <body>
    <h1>Hello!</h1>
  </body>
</html>




运行应用程序将应用程序发布到 Tomcat 容器,再通过 http://localhost:8080/CartApp/spring/index.jsp 访问 index.jsp 页面(应用程序所在文件夹名是 CartApp ),测试 Spring Web MVC 配置是否正确。如果一切正常,可得到如下页面:
图 3 显示结果配置 Spring Web Flow 2.0 的基础配置好 Spring Web MVC 的环境后,接下来就可以往里面加入 Spring Web Flow 2.0 的配置。不过,要搞明白 Spring Web Flow 2.0 的配置,必须先要了解相关的理论知识。
FlowRegistryFlowRegistry 是存放 flow 的仓库,每个定义 flow 的 XML 文档被解析后,都会被分配一个唯一的 id ,并以 FlowDefinition 对象的形式存放在 FlowResigtry 中。 FlowRegistry 配置方式可参看清单 8。
说明以下的示例清单中的 XML 配置元素默认使用了 webflow 名字空间,这也是 Spring Web Flow 习惯上的名字空间,参看教程后面 webflow-config.xml 文件,可以更多了解 webflow 名字空间。

清单 8 FlowRegistry 的配置<webflow:flow-registry id="flowRegistry">
    <webflow:flow-location path="/WEB-INF/flows/shopping.xml" id=”shopping”/>
</webflow:flow-registry>
每个 flow 都必须要有 id 来标识,如果在配置中省略,那么该 flow 默认的 id 将是该定义文件的文件名去掉后缀所得的字符串。
FlowExecutorFlowExecutor 是 Spring Web Flow 的一个核心接口,启动某个 flow ,都要通过这个接口来进行。从配置角度来说,只要保证有个 FlowExecutor 就可以了, Spring Web Flow 的默认行为已经足够。默认配置参看清单9。
清单 9 FlowExecutor 的配置
1
<webflow:flow-executor id="flowExecutor" />




哪个 flow 被执行了?FlowRegistry 中注册的 flow 可能会有多个,但前面介绍过,每个 flow 都会有 id ,没有配置的,也会有个默认值, FlowExecutor 就是通过 id 来找出要执行的 flow 。至于这个 id ,则是要由用户来指定的。在默认配置情况下,如果客户端发送了如下URL请求:
1
http://localhost:8080/CartApp/spring/shopping




则从 Spring Web Flow 的角度来看,这个 URL 就表示客户想要执行一个 id 为“ shopping ”的 flow ,于是就会在 FlowRegistry 中查找名为“ shopping ”的 flow,由FlowExecutor负责执行。
返回列表