我将如何从Spring MVC Web应用程序中的任何一点进入Web流程? Spring Web Flow webapp无法正常工作

时间:2014-02-16 08:22:20

标签: spring spring-mvc spring-webflow

我一直在尝试使用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'现在只是打印混乱。

这是web app控制器'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");
    }

}

Preceding是网络应用代码。 现在我想谈谈我的问题: 1.我将如何从Spring MVC Web应用程序的任何一点进入Web流程?我的意思是如何点击'index.jsp'中的以下链接进入网络流程:

<a href="userRegistrationStartPage.htm">User Registration</a>
  1. 上面提到的webapp无法正常工作,我还需要添加或更改哪些内容才能使其正常工作?
  2. 我真的很期待从一个人那里听到答案。

    感谢。

1 个答案:

答案 0 :(得分:2)

你快到了:

最佳做法是将流的所有元素添加到一个文件夹中。因此,将名为“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对象来存储注册信息,所以您需要在流的开头创建一个。请参阅流程开始时需要Hotel对象的spring webflow的booking-mvc example

<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,因此您可以将其删除。

更新

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

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