下载spring2.5.5和spring web flow2.0.3。
新建一个工程,在WEB-INF/lib目录下添加如下的jar文件:
- commons-logging.jar
- ognl-2.6.11.jar
- org.springframework.binding-2.0.3.RELEASE.jar
- org.springframework.faces-2.0.3.RELEASE.jar
- org.springframework.js-2.0.3.RELEASE.jar
- org.springframework.webflow-2.0.3.RELEASE.jar
- spring.jar
- spring-webmvc.jar
然后需要配置一下web.xml,加上SpringMVC的支持,配置很简单,不多加解释了,下面是代码:
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
- id="WebApp_ID" version="2.5">
- <display-name>helloswf</display-name>
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/simple-servlet.xml</param-value>
- </context-param>
- <servlet>
- <servlet-name>simple</servlet-name>
- <servlet-class>
- org.springframework.web.servlet.DispatcherServlet
- </servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>simple</servlet-name>
- <url-pattern>*.html</url-pattern>
- </servlet-mapping>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
配置simple-servlet.xml这个spring的上下文定义文件:
- <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"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/webflow-config
- http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.0.xsd">
- <bean id="publicUrlMappings"
- class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
- <property name="mappings">
- <value>/helloWorld.html=helloWorldHandler</value>
- </property>
- </bean>
- <bean id="helloWorldHandler"
- class="org.springframework.webflow.mvc.servlet.AbstractFlowHandler" />
- <bean
- class="org.springframework.webflow.mvc.servlet.FlowHandlerAdapter">
- <property name="flowExecutor" ref="flowExecutor"/>
- </bean>
- <!-- Spring Web Flow Configuration -->
- <webflow:flow-executor id="flowExecutor"
- flow-registry="flowRegistry" />
- <webflow:flow-registry id="flowRegistry">
- <webflow:flow-location path="/WEB-INF/flows/helloWorld.xml" />
- </webflow:flow-registry>
- </beans>
通过SimpleUrlHandlerMapping这个URL映射器将/helloWorld.html这个URL交给org.springframework.webflow.mvc.servlet.AbstractFlowHandler去处理。
定义flowRegistry、flowExecutor和FlowHandlerAdapter,并在flowRegistry中注册/WEB-INF/flows/helloWorld.xml这个流程定义文件。
下面是/WEB-INF/flows/helloWorld.xml这个文件的内容:
- <?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="hello">
- <transition on="*" to="helloWorld" />
- </view-state>
- <end-state id="helloWorld" view="helloWorld.jsp" />
- </flow>
这个流程中使用了两个视图,hello和helloWorld,我们在/WEB-INF/flows/这个目录下新建这两个文件。
hello.jsp:
- <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html>
- <head>
- <title>Welcome to Spring Web Flow</title>
- </head>
- <body>
- <h1>Welcome to Spring Web Flow</h1>
- <form:form id="start">
- <input type="submit" name="_eventId" value="Click to say hello!" />
- </form:form>
- </body>
- </html>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html>
- <head>
- <title>Welcome to Spring Web Flow</title>
- </head>
- <body>
- <h1>Hello, Web Flow!</h1>
- </body>
- </html>
注意:在hello.jsp中一定要使用spring的form标签。如果使用html的form标签,要使用下面的写法:
- <form id="start" action="${flowExecutionUrl}" method="post">
好了,启动tomcat,然后再浏览器中输入:http://localhost:8080/helloswf/helloWorld.html测试一下吧。
1 评论:
2008年9月16日 09:53
http://java.dzone.com/articles/spring-web-flow-crud-tutorial
这是一个使用SWF写的CRUD的一个例子,里边用到了与JSF的结合以及子流程等知识点,麻雀虽小五脏俱全。
发表评论