当前位置: 代码迷 >> Web前端 >> Spring WebFlow(二)Learn from the Sample
  详细解决方案

Spring WebFlow(二)Learn from the Sample

热度:2158   发布时间:2012-07-27 11:03:01.0
Spring WebFlow(2)Learn from the Sample
Spring WebFlow(2)Learn from the Sample

Download the zip file with URL
http://s3.amazonaws.com/dist.springframework.org/release/SWF/spring-webflow-2.3.1.RELEASE.zip

1. webflow-showcase
The sample projects are here: spring-webflow-2.3.1.RELEASE\projects\spring-webflow-samples

>mkdir spring-samples
>cd spring-samples
>svn co https://src.springframework.org/svn/spring-samples/webflow-primefaces-showcase/
>svn co https://src.springframework.org/svn/spring-samples/webflow-richfaces-showcase/
>svn co https://src.springframework.org/svn/spring-samples/webflow-showcase/

Once the project is in my local machine, I will use this maven command
>mvn clean install tomcat:run
I will learn the sample webflow-showcase with this URL http://localhost:8080/webflow-showcase.

But there is nothing special in the sample.

2. Booking-mvc
I will learn the sample booking-mvc from D:\book\webflow\spring-samples\booking-mvc
>mvn clean install tomcat:run
http://localhost:8080/booking-mvc/.

Tip 1:
Change the servlet configuration in web.xml from
<servlet>
<servlet-name>easywebflow</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

to

<servlet>
<servlet-name>easywebflow</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

Then, we do not need the configuration file easywebflow-servlet.xml.

error message:
Caused by: javax.validation.ValidationException: Unable to find a default provider

solution:
<!-- JSR 303 with Hibernate Validator -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.1.0.Beta1</version>
</dependency>

error message:
java.lang.NullPointerException
org.springframework.security.access.vote.RoleVoter.extractAuthorities(RoleVoter.java:115)

solution:
Because there is no authority in the current user. Just logon as one user with authority.

Tip 2:
Configure the current webflow with spring security with these files:
web.xml
<servlet>
<servlet-name>easywebflow</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Enables Spring Security -->
<filter>
       <filter-name>springSecurityFilterChain</filter-name>
       <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
      <filter-name>springSecurityFilterChain</filter-name>
      <servlet-name>easywebflow</servlet-name>
</filter-mapping>

security-context.xml:
<?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:security="http://www.springframework.org/schema/security"
       xsi:schemaLocation="
           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd">

<security:http auto-config="true">
<security:form-login login-page="/login.do" login-processing-url="/loginProcess.do"
default-target-url="/shopping.do" authentication-failure-url="/login.do?login_error=1" />
<security:logout logout-url="/logout.do" logout-success-url="/logoutSuccess.do" />
</security:http>

<security:authentication-manager>
<security:authentication-provider>
<security:user-service>
<security:user name="karl" password="111111" authorities="ROLE_USER, ROLE_SUPERVISOR" />
<security:user name="kiko" password="111111" authorities="ROLE_USER" />
</security:user-service>
</security:authentication-provider>
</security:authentication-manager>
</beans>

controller-context.xml:
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:lang="http://www.springframework.org/schema/lang" xmlns:webflow="http://www.springframework.org/schema/webflow-config"
xmlns:groovy="http://www.sillycat.com/schema/groovy"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/lang
http://www.springframework.org/schema/lang/spring-lang-3.0.xsd
http://www.springframework.org/schema/webflow-config
http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.0.xsd
http://www.sillycat.com/schema/groovy
http://www.sillycat.com/schema/groovy/groovy.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
">
...snip...
<mvc:view-controller path="/login.do" view-name="login"/>
<mvc:view-controller path="/logoutSuccess.do" view-name="logoutSuccess"/>
...snip...

shopping.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">

<secured attributes="ROLE_USER" />
...snip...
<subflow-state id="addProductToCart" subflow="addToCart">
<secured attributes="ROLE_SUPERVISOR"/>
<transition on="productAdded" to="viewCart">
</transition>
</subflow-state>

And there is also validation example in booking-mvc, but I am using velocity as my template file. There may be no JSP tag support for that. So currently I did not consider spring form tag and Spring.addDecorator

references:
http://www.springsource.org/spring-web-flow
http://static.springsource.org/spring-webflow/docs/2.3.x/reference/html/index.html
http://zhidao.baidu.com/question/324839954.html?seed=0
http://forum.springsource.org/showthread.php?63623-getting-taglibs-wrapper-to-work-in-velocity-views
http://freemarker.sourceforge.net/docs/pgui_misc_servlet.html#autoid_56


  相关解决方案