完整实例下载 springwebflow.rar
购物车用例
下面就讲解如何进行配置:
引入jar包
spring mvc需要的:
org.springframework.asm-3.0.5.RELEASE.jar
org.springframework.beans-3.0.5.RELEASE.jar
org.springframework.context-3.0.5.RELEASE.jar
org.springframework.core-3.0.5.RELEASE.jar
org.springframework.expression-3.0.5.RELEASE.jar
org.springframework.web-3.0.5.RELEASE.jar
org.springframework.web.servlet-3.0.5.RELEASE.jar
commons-lang-2.1.jar
commons-logging-1.1.jar
spring webflow需要的
spring-binding-2.3.1.RELEASE.jar
spring-js-2.3.1.RELEASE.jar
spring-webflow-2.3.1.RELEASE.jar
可以到这下载:http://maven.springframework.org/release/org/springframework/spring/
一.spring mvc配置
web.xml配置
01.
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
02.
<
web-app
version
=
"2.4"
03.
xmlns
=
"http://java.sun.com/xml/ns/j2ee"
04.
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
05.
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
06.
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
07.
08.
<
servlet
>
09.
<
servlet-name
>spring3</
servlet-name
>
10.
<
servlet-class
>org.springframework.web.servlet.DispatcherServlet</
servlet-class
>
11.
<
load-on-startup
>1</
load-on-startup
>
12.
</
servlet
>
13.
<
servlet-mapping
>
14.
<
servlet-name
>spring3</
servlet-name
>
15.
<
url-pattern
>*.html</
url-pattern
>
16.
</
servlet-mapping
>
17.
</
web-app
><
span
style
=
"font-family:'sans serif, tahoma, verdana, helvetica';"
><
span
style
=
"white-space:normal;"
> </
span
></
span
>
为啥文件名是spring3-servlet.xml。其中spring3是上面servlet name的名字。命名规则:servletName-servlet.xml
01.
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
02.
<
beans
xmlns
=
"http://www.springframework.org/schema/beans"
03.
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
04.
xmlns:p
=
"http://www.springframework.org/schema/p"
05.
xmlns:context
=
"http://www.springframework.org/schema/context"
06.
xmlns:webflow
=
"http://www.springframework.org/schema/webflow-config"
07.
xsi:schemaLocation="
08.
http://www.springframework.org/schema/beans
09.
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
10.
http://www.springframework.org/schema/context
11.
http://www.springframework.org/schema/context/spring-context-3.0.xsd
12.
http://www.springframework.org/schema/webflow-config
13.
http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.0.xsd">
14.
15.
16.
<!-- spring mvc 基础组件配置 -->
17.
<
context:component-scan
base-package
=
"com.controller"
/>
18.
<
bean
id
=
"viewResolver"
class
=
"org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix
=
"/view/"
p:suffix
=
".jsp"
/>
19.
20.
<
bean
class
=
"org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"
/>
21.
22.
</
beans
>
在上面spring3-servlet.xml 文件中添加如下组件:
01.
<!-- webflow配置 -->
02.
<
bean
name
=
"flowController"
class
=
"org.springframework.webflow.mvc.servlet.FlowController"
>
03.
<
property
name
=
"flowExecutor"
ref
=
"flowExecutor"
/>
04.
</
bean
>
05.
<
bean
id
=
"urlMapping"
class
=
"org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"
>
06.
<
property
name
=
"mappings"
>
07.
<
props
>
08.
<
prop
key
=
"/shopping.html"
>flowController</
prop
>
09.
</
props
>
10.
</
property
>
11.
</
bean
>
12.
<
webflow:flow-executor
id
=
"flowExecutor"
/>
13.
<
webflow:flow-registry
id
=
"flowRegistry"
flow-builder-services
=
"flowBuilderServices"
>
14.
<
webflow:flow-location
path
=
"/WEB-INF/shopping.xml"
id
=
"shopping"
/>
15.
</
webflow:flow-registry
>
16.
<
webflow:flow-builder-services
id
=
"flowBuilderServices"
view-factory-creator
=
"mvcViewFactoryCreator"
/>
17.
<
bean
id
=
"mvcViewFactoryCreator"
class
=
"org.springframework.webflow.mvc.builder.MvcViewFactoryCreator"
>
18.
<
property
name
=
"viewResolvers"
ref
=
"viewResolver"
/>
19.
</
bean
>
其中 flowController 是接收webflow请求的控制器。urlMapping配置请求的映射,将地址/shopping.html映射到上面的flowController控制器中。
FlowExecutor 是 Spring Web Flow 的一个核心接口,启动某个 flow ,都要通过这个接口来进行。从配置角度来说,只要保证有个 FlowExecutor 就可以了, Spring Web Flow 的默认行为已经足够。
FlowRegistry 是存放 flow 的仓库,每个定义 flow 的 XML 文档被解析后,都会被分配一个唯一的 id ,并以 FlowDefinition 对象的形式存放在 FlowResigtry 中。
flow-builder-services 属性的配置指明了在这个 flow-registry “仓库”里的 flow 的一些基本特性,例如,是用 Unified EL 还是 OGNL 、 model (模型)对象中的数据在显示之前是否需要先作转换,等等。在本示例中,我们需要在 flow-builder-services 属性中指明 Spring Web Flow 中所用到的 view ,由 Spring Web MVC 的“ View Resolver ”来查找,由 Spring Web MVC 的“ View Class”来解析,最后呈现给客户
创建webflow的流程描述文件shopping.xml
01.
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
02.
<
flow
xmlns
=
"http://www.springframework.org/schema/webflow"
03.
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
04.
xsi:schemaLocation="http://www.springframework.org/schema/webflow
05.
http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd">
06.
07.
<
view-state
id
=
"viewCart"
view
=
"viewCart"
>
08.
<
transition
on
=
"submit"
to
=
"viewOrder"
>
09.
</
transition
>
10.
</
view-state
>
11.
<
view-state
id
=
"viewOrder"
view
=
"viewOrder"
>
12.
<
transition
on
=
"confirm"
to
=
"orderConfirmed"
>
13.
</
transition
>
14.
</
view-state
>
15.
<
view-state
id
=
"orderConfirmed"
view
=
"orderConfirmed"
>
16.
<
transition
on
=
"returnToIndex"
to
=
"returnToIndex"
>
17.
</
transition
>
18.
</
view-state
>
19.
<
end-state
id
=
"returnToIndex"
view
=
"viewEnd"
>
20.
</
end-state
>
21.
</
flow
>
Spring Web Flow 的基本元素
Flow 可看作是客户端与服务器的一次对话( conversation )。 Flow 的完成要由分多个步骤来实现,在 Spring Web Flow 的语义中,步骤指的就是 state 。Spring Web Flow 提供了五种 state ,分别是 Action State 、 View State 、 Subflow State 、 Decision State 、 End State ,这些 state 可用于定义 flow 执行过程中的各个步骤。除了 End State 外,其他 state 都可以转换到别的 state ,一般通过在 state 中定义 transition 来实现到其他 state 的转换,转换的发生一般由事件( event )来触发。
webroot下创建view文件夹,view下创建四个流程页面:
orderConfirmed.jsp
01.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
02.
pageEncoding="ISO-8859-1"%>
03.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
04.
<
html
>
05.
<
head
>
06.
<
meta
http-equiv
=
"Content-Type"
content
=
"text/html; charset=ISO-8859-1"
>
07.
<
title
>Insert title here</
title
>
08.
</
head
>
09.
<
body
>
10.
<
h1
>Order Confirmed</
h1
>
11.
<
a
href
=
"${flowExecutionUrl}&_eventId=returnToIndex"
>Return to index</
a
>
12.
</
body
>
13.
</
html
>
01.
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
02.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
03.
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
04.
<
html
xmlns
=
"http://www.w3.org/1999/xhtml"
>
05.
<
head
>
06.
<
meta
http-equiv
=
"Content-Type"
content
=
"text/html; charset=utf-8"
/>
07.
<
title
>View Cart</
title
>
08.
</
head
>
09.
<
body
>
10.
<
h1
>spring webflow购物车演示</
h1
>
11.
<
a
href
=
"${flowExecutionUrl}&_eventId=submit"
>Submit</
a
>
12.
</
body
>
13.
</
html
>
01.
<%@ page language="java" contentType="text/html; charset=utf-8"
02.
pageEncoding="utf-8"%>
03.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
04.
<
html
>
05.
<
head
>
06.
<
meta
http-equiv
=
"Content-Type"
content
=
"text/html; charset=utf-8"
>
07.
<
title
>Insert title here</
title
>
08.
</
head
>
09.
<
body
>
10.
非常好,流程结束
11.
</
body
>
12.
</
html
>
01.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
02.
pageEncoding="ISO-8859-1"%>
03.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
04.
<
html
>
05.
<
head
>
06.
<
meta
http-equiv
=
"Content-Type"
content
=
"text/html; charset=ISO-8859-1"
>
07.
<
title
>Insert title here</
title
>
08.
</
head
>
09.
<
body
>
10.
<
h1
>Order</
h1
>
11.
<
a
href
=
"${flowExecutionUrl}&_eventId=confirm"
>Confirm</
a
>
12.
</
body
>
13.
</
html
>
ok,完毕,大家可以下载完整实例。
之后再浏览器中输入如下地址即可演示: http://localhost:8080/springwebflow/shopping.html 当然端口号根据你的