【问题标题】:How I will enter into web flow from any point in Spring MVC web app? Spring Web Flow webapp is not working我将如何从 Spring MVC Web 应用程序中的任何点进入 Web 流? Spring Web Flow webapp 不工作
【发布时间】:2014-03-15 13:21:51
【问题描述】:

我一直在尝试使用 Spring Web Flow 开发一个多页用户注册表单,但无法完成。稍后我将在这篇文章中粘贴我的应用程序代码。 我会感谢那些发现缺失部分或错误并指导我解决问题的人。

我的网络应用程序名称是“UserRegistrationSWF”。目录结构如下:

UserRegistrationSWF
     -Java Resources
         -src
            -org.nitest.controller
                -UserRegistrationController.java
            -org.nitesh.model
                -User.java
     -WebContent
         -WEB-INF
            -config
                -swf-config.xml
                -web-application-config.xml
            -swf
                -swf-flow.xml
                -userRegistrationPage2.jsp
                -userRegistrationLastPage.jsp
            -view
                -cancel.jsp
                -success.jsp
                -userRegistrationStartPage.jsp
            -web.xml     
         -index.jsp

我正在使用 Spring MVC,我的欢迎页面是“index.jsp”。代码如下:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Welcome Page</title>
</head>
<body>
<a href="userRegistrationStartPage.htm">User Registration</a>
</body>
</html>

这里是“web.xml”代码:

<?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_3_0.xsd" id="WebApp_ID" version="3.0">

  <display-name>UserRegistrationSWF</display-name>

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

  <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/config/web-application-config.xml</param-value>
  </context-param>

  <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <servlet>
    <servlet-name>User Registration</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>

  <servlet-mapping>
    <servlet-name>User Registration</servlet-name>
    <url-pattern>*.htm</url-pattern>
  </servlet-mapping>

</web-app>

这里是“web-application-config.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:webflow="http://www.springframework.org/schema/webflow-config"
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/webflow-config
http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.3.xsd">

    <mvc:annotation-driven/>
    <context:component-scan base-package="org.nitesh" />

    <bean id="jspViewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/view/" />
        <property name="suffix" value=".jsp" />
    </bean>

<import resource="swf-config.xml"/> 

</beans>

这里是“swf-config.xml”代码:

<?xml version="1.0" encoding="UTF-8"?>
<beans xsi:schemaLocation=" http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
http://www.springframework.org/schema/webflow-config 
http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.3.xsd" 
xmlns:webflow="http://www.springframework.org/schema/webflow-config" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://www.springframework.org/schema/beans">

<webflow:flow-registry id="flowRegistry" base-path="/WEB-INF">
    <webflow:flow-location path="/swf/swf-flow.xml" />
</webflow:flow-registry>

<webflow:flow-executor id="flowExecutor" flow-registry="flowRegistry">
</webflow:flow-executor>

<bean class="org.springframework.webflow.mvc.servlet.FlowHandlerAdapter">
    <property name="flowExecutor" ref="flowExecutor" />
</bean>

<bean class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping">
    <property name="flowRegistry" ref="flowRegistry"/>
</bean>

</beans>

这里是“swf-flow.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-config
http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.3.xsd">

<view-state id="userRegistrationStartPage" view="userRegistrationStartPage.htm" model="user">
<transition on="cancel" to="cancel"></transition>
<transition on="proceed" to="userRegistrationPage2"></transition>
</view-state>

<view-state id="userRegistrationPage2" view="userRegistrationPage2.htm">
<transition on="revise" to="userRegistrationStartPage"></transition>
<transition on="proceed" to="userRegistrationLastPage"></transition>
<transition on="cancel" to="cancel"></transition>
</view-state>

<view-state id="userRegistrationLastPage" view="userRegistrationLastPage.htm">
<transition on="revise" to="userRegistrationPage2"></transition>
<transition on="confirm" to="success"></transition>
<transition on="cancel" to="cancel"></transition>
</view-state>

<end-state id="success" view="swf/success.jsp"></end-state>

<end-state id="cancel" view="swf/cancel.jsp"></end-state>

</flow>

这里是“userRegistrationStartPage.jsp”代码:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>User Registration Start Page</title>
</head>
<body>
<form:form commandName="user" method="POST">

<table>
<tr>
<td>Name:</td>
<td><form:input path="name"/></td>
</tr>

<tr>
<td>Email:</td>
<td><form:input path="email"/></td>
</tr>

<tr>
<td>Password:</td>
<td><form:password path="password"/></td>
</tr>

<tr>
<td><input type="submit" name="_eventId_cancel" value="Cancel"></td>
<td><input type="submit" name="_eventId_proceed" value="Next"></td>
</tr>

</table>
</form:form>
</body>
</html>

“cancel.jsp”和“success.jsp”分别简单地打印取消和成功消息。 'User' 类是一个 userRegistrationStartPage 表单 bean 类。 'userRegistrationPage2.jsp' 和 'userRegistrationLastPage.jsp' 现在只打印消息。

这是网络应用程序控制器“UserRegistrationController.java”的代码:

package org.nitesh.controller;

import org.nitesh.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class UserRegistrationController {

    @RequestMapping(value = "userRegistrationStartPage.htm")
    public ModelAndView showUserRegistrationFormStartPage(@ModelAttribute("user") User user)
    {
        return new ModelAndView("userRegistrationStartPage");
    }

}

前面是网络应用程序代码。 现在我想提出我的问题: 1. 我将如何从 Spring MVC Web 应用程序中的任何点进入 Web 流?我的意思是我将如何通过单击“index.jsp”中的以下链接进入网络流:

<a href="userRegistrationStartPage.htm">User Registration</a>
  1. 上述 webapp 无法正常工作,我还需要添加或更改哪些内容才能使其正常工作?

我真的很期待听到一个人的回答。

谢谢。

【问题讨论】:

    标签: spring spring-mvc spring-webflow


    【解决方案1】:

    你快到了:

    最好将流程的所有元素添加到一个文件夹中。因此,将名为“userRegistrationStartPage.jsp”的流程的第一页从 /view/ 移动到 /swf/

    -WebContent
        -WEB-INF
            -config
                -swf-config.xml
                -web-application-config.xml
            -swf
                -swf-flow.xml
                -userRegistrationStartPage.jsp
                -userRegistrationPage2.jsp
                -userRegistrationLastPage.jsp
            -view
                -cancel.jsp
                -success.jsp                
            -web.xml     
         -index.jsp
    

    因为您在 swf-config.xml 中定义了一个流程

    <webflow:flow-registry id="flowRegistry" base-path="/WEB-INF">
        <webflow:flow-location path="/swf/swf-flow.xml" />
    </webflow:flow-registry>
    

    启动该流程的映射将是 swf(路径的第一部分) 将您的 index.jsp 更改为以下内容

    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
            <title>Welcome Page</title>
        </head>
        <body>
            <a href="swf">User Registration</a>
        </body>
    </html>
    

    此外,由于您使用 User 对象来存储注册信息,因此您需要在流程开始时创建一个。请参阅 spring webflow 的 booking-mvc example,其中在流程开始时需要 Hotel 对象:

    <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">
    
        <input name="hotelId" required="true" />
    
        <on-start>
            <evaluate expression="bookingService.createBooking(hotelId, currentUser.name)" result="flowScope.booking" />
        </on-start>
    
        <view-state id="enterBookingDetails" model="booking">
            etc...
        </view-state>
    </flow>
    

    在这里,他们使用一项服务从数据库 YMMV 中获取酒店。

    最后,不需要 UserRegistrationController,因此您可以将其移除。

    更新

    在流程开始时创建新用户对象的最快方法是在 swf-flow.xml 的开头添加以下行:

    <on-start>
        <evaluate expression="new org.nitesh.model.User()" result="flowScope.user" />
    </on-start>
    

    【讨论】:

    • 非常感谢您的解释。它帮助了我,现在我能够执行 Web 流程,但我想请您详细说明如何在流程开始时创建,在这种情况下,用户对象。我试图从 spring-mvc 示例中获取它,但没有得到。
    猜你喜欢
    • 2014-02-26
    • 2011-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-24
    • 2010-10-18
    相关资源
    最近更新 更多