Spiga

Spring Web Flow快速上手

下载spring2.5.5spring 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的支持,配置很简单,不多加解释了,下面是代码:


  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  5. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  6. id="WebApp_ID" version="2.5">
  7. <display-name>helloswf</display-name>
  8. <context-param>
  9. <param-name>contextConfigLocation</param-name>
  10. <param-value>/WEB-INF/simple-servlet.xml</param-value>
  11. </context-param>
  12. <servlet>
  13. <servlet-name>simple</servlet-name>
  14. <servlet-class>
  15. org.springframework.web.servlet.DispatcherServlet
  16. </servlet-class>
  17. <load-on-startup>1</load-on-startup>
  18. </servlet>
  19. <servlet-mapping>
  20. <servlet-name>simple</servlet-name>
  21. <url-pattern>*.html</url-pattern>
  22. </servlet-mapping>
  23. <welcome-file-list>
  24. <welcome-file>index.jsp</welcome-file>
  25. </welcome-file-list>
  26. </web-app>


配置simple-servlet.xml这个spring的上下文定义文件:


  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns:webflow="http://www.springframework.org/schema/webflow-config"
  4. xsi:schemaLocation="
  5. http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  7. http://www.springframework.org/schema/webflow-config
  8. http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.0.xsd">
  9. <bean id="publicUrlMappings"
  10. class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
  11. <property name="mappings">
  12. <value>/helloWorld.html=helloWorldHandler</value>
  13. </property>
  14. </bean>
  15. <bean id="helloWorldHandler"
  16. class="org.springframework.webflow.mvc.servlet.AbstractFlowHandler" />
  17. <bean
  18. class="org.springframework.webflow.mvc.servlet.FlowHandlerAdapter">
  19. <property name="flowExecutor" ref="flowExecutor"/>
  20. </bean>
  21. <!-- Spring Web Flow Configuration -->
  22. <webflow:flow-executor id="flowExecutor"
  23. flow-registry="flowRegistry" />
  24. <webflow:flow-registry id="flowRegistry">
  25. <webflow:flow-location path="/WEB-INF/flows/helloWorld.xml" />
  26. </webflow:flow-registry>
  27. </beans>
  • 通过SimpleUrlHandlerMapping这个URL映射器将/helloWorld.html这个URL交给org.springframework.webflow.mvc.servlet.AbstractFlowHandler去处理。

  • 定义flowRegistryflowExecutorFlowHandlerAdapter,并在flowRegistry中注册/WEB-INF/flows/helloWorld.xml这个流程定义文件。


下面是/WEB-INF/flows/helloWorld.xml这个文件的内容:


  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <flow xmlns="http://www.springframework.org/schema/webflow"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/webflow
  5. http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd">
  6. <view-state id="hello">
  7. <transition on="*" to="helloWorld" />
  8. </view-state>
  9. <end-state id="helloWorld" view="helloWorld.jsp" />
  10. </flow>


这个流程中使用了两个视图,hellohelloWorld,我们在/WEB-INF/flows/这个目录下新建这两个文件。


hello.jsp:

  1. <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  3. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4. <html>
  5. <head>
  6. <title>Welcome to Spring Web Flow</title>
  7. </head>
  8. <body>
  9. <h1>Welcome to Spring Web Flow</h1>
  10. <form:form id="start">
  11. <input type="submit" name="_eventId" value="Click to say hello!" />
  12. </form:form>
  13. </body>
  14. </html>
helloWorld.jsp:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html>
  4. <head>
  5. <title>Welcome to Spring Web Flow</title>
  6. </head>
  7. <body>
  8. <h1>Hello, Web Flow!</h1>
  9. </body>
  10. </html>

注意:在hello.jsp中一定要使用spring的form标签。如果使用html的form标签,要使用下面的写法:

  1. <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的结合以及子流程等知识点,麻雀虽小五脏俱全。