Spring MVC 複雜表單分部提交--使用AbstractWizardFormController解決方案

如果我們的表單有很多的輸入選項,如果都放置在一個頁面上,會導致頁面過多,如果我們能把這些輸入分散到幾個頁面上,按嚮導的方式填寫,在最後一頁上進行提交,勢必會帶來良好的可操作性,使用spring MVC 架構中的AbstractWizardFormController控制器,可以很輕鬆的完成以上功能

(1)配置文件:web.xml 

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
    xmlns
="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation
="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
>
  
<context-param>
    
<param-name>contextConfigLocation</param-name>
    
<param-value>/WEB-INF/train-servlet.xml</param-value>
  
</context-param>
  
<servlet>
    
<servlet-name>train</servlet-name>
    
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    
<load-on-startup>0</load-on-startup>
  
</servlet>
  
<servlet-mapping>
     
<servlet-name>train</servlet-name>
     
<url-pattern>*.mvc</url-pattern>
  
</servlet-mapping>

   
<listener>
     
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
   
</listener>
    
  
<filter>
    
<filter-name>character</filter-name>
    
<filter-class>Action.CharacterFilter</filter-class>
  
</filter>
  
<filter-mapping>
    
<filter-name>character</filter-name>
    
<url-pattern>/*</url-pattern>
  
</filter-mapping>
  
<welcome-file-list>
    
<welcome-file>index.jsp</welcome-file>
  
</welcome-file-list>
</web-app>

 

(2)控制器類

   其中successView是最後表單提交成功後的回顯頁面,由spring注入
           cancelView是中途取消提交過程後的返回頁面,由spring注入
           vote爲我們表單對應的javabean
   
         繼承了AbstractWizardFormController,所以必須實現他的abstract mothed---processFinish。也就是說,若果繼承了AbstractWizardFormController,你僅需要實現這個方法。當所有的頁面表單填寫完將調用這個方法。processCancel方法不是必須實現的,他是在你填寫某一步表單時想取消,按取消按鈕時調用。

package Action;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Vote;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractWizardFormController;
import org.springframework.web.util.WebUtils;

public class FeedBackWizardController extends AbstractWizardFormController {

    
private String successView;
    
private String cancelView;
    
public String getCancelView() {
        
return cancelView;
    }


    
public void setCancelView(String cancelView) {
        
this.cancelView = cancelView;
    }


    
public String getSuccessView() {
        
return successView;
    }


    
public void setSuccessView(String successView) {
        
this.successView = successView;
    }


    
protected ModelAndView processCancel(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, BindException arg3) throws Exception {

       
return new ModelAndView(this.getCancelView());
    }

 

    
protected ModelAndView processFinish(HttpServletRequest request,
            HttpServletResponse response, Object object, BindException exception)
            
throws Exception {
        Vote vote
=(Vote)object;
        
        
return new ModelAndView(this.getSuccessView(),"vote",vote);
    }


}

JavaBean:

package model;
public class Vote {
   
private String id;
   
private String name;
   
private String option;
   
private String result;
public String getId() {
    
return id;
}

public void setId(String id) {
    
this.id = id;
}

public String getName() {
    
return name;
}

public void setName(String name) {
    
this.name = name;
}

public String getOption() {
    
return option;
}

public void setOption(String option) {
    
this.option = option;
}

public String getResult() {
    
return result;
}

public void setResult(String result) {
    
this.result = result;
}
 
}

 

配置文件:

 

<bean id="simpleUrlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
 
<property name="mappings">
   
<props>
     
<prop key="/feedback.mvc">FeedbackController</prop>
 
   
</props>
 
</property>
</bean>



<!-- 使用AbstractWizardFormController控制器 -->
<bean id="FeedbackController" class="Action.FeedBackWizardController">
  
<property name="successView"><value>formWizard/thankyou</value> </property>
  
<property name="cancelView"><value>formWizard/first</value> </property> 
  
<property name="commandClass"><value>model.Vote</value></property>  <!---配置操作類->

  <property name="pages">
    <list>  <!---此處定義表單嚮導的頁面流順序,要嚴格執行這裏配置的順序->
      <value>formWizard/first</value>
      <value>formWizard/id</value>
      <value>formWizard/name</value>
      <value>formWizard/option</value>
      <value>formWizard/result</value>
    </list>
  </property>
</bean>


<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix">
    <value>/</value>
  </property>
  <property name="suffix">
    <value>.jsp</value>
  </property>
</bean>

我們執行feedback.mvc時候,默認首先訪問第一順位的頁面first.jsp

first.jsp:

 

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding
="GB18030"
%>
<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
<FORM action="feedback.mvc" method="post">
 
<TABLE>
  
<TBODY>
   
<TR>
    
<TD>
     註冊信息,請認真填寫!
     
<INPUT type="submit" value="開始" name="_target1"/>
    
</TD>
   
</TR>
  
</TBODY>
 
</TABLE>
</FORM>
</body>
</html>

id.jsp

 

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  
<head>
    
   
 
  
</head>
  
  
  
<body>
  
<spring:bind path="command.id">
   
<form action="feedback.mvc" method="post"> 
     id: 
<input type="text" name="id" value="<c:out value="${status.value}"/>"/>

     
<input type="submit" value="下一步" name="_target2" /> 

     
<input type="submit" value="取消" name="_cancel"/>  
      
<input type="submit" value="完成" name="_finish"/> 
   
</form>
   
</spring:bind>
  
</body>
</html>

name.jsp

 

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  
<head>
    
   
 
  
</head>
  
  
  
<body>
    
<spring:bind path="command.name">
       
<form action="feedback.mvc" method="post">
     name: 
<input type="text" name="name" value="<c:out value="${status.value}"/>"/>
     
<input type="submit" value="上一步" name="_target1"/>   
     
<input type="submit" value="下一步" name="_target3"/>
      
<input type="submit" value="取消" name="_cancel"/>     
   
</form>
   
</spring:bind>
  
</body>
</html>

 

option.jsp

 

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  
<head>
    
   
 
  
</head>
  
  
  
<body>
 
<spring:bind path="command.option">
      
<form action="feedback.mvc" method="post">
     option: 
<input type="text" name="option" value="<c:out value="${status.value}"/>"/>
     
<input type="submit" value="上一步" name="_target2"/>   
     
<input type="submit" value="下一步" name="_target4"/> 
 
<input type="submit" value="取消" name="_cancel"/>   
   
</form>
   
</spring:bind>
  
</body>
</html>

 

result.jsp

 

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  
<head>
    
   
 
  
</head>
  
  
  
<body>
<spring:bind path="command.result">
     
<form action="feedback.mvc" method="post">
     result: 
<input type="text" name="result" value="<c:out value="${status.value}"/>"/>
     
<input type="submit" value="上一步" name="_target3"/> 
     
<input type="submit" value="完成" name="_finish"/> 
     
<input type="submit" value="取消" name="_cancel"/>    
   
</form>
   
</spring:bind>
  
</body>
</html>

 

thankyou.jsp

 

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  
<head>
    
<base href="<%=basePath%>">
    
    
<title>My JSP 'index.jsp' starting page</title>
    
<meta http-equiv="pragma" content="no-cache">
    
<meta http-equiv="cache-control" content="no-cache">
    
<meta http-equiv="expires" content="0">    
    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    
<meta http-equiv="description" content="This is my page">
    
<!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    
-->
  
</head>
  
  
<body>
    ${vote.id}-----${vote.name}-----${vote.option}---${vote.result }
<br>
  
</body>
</html>

 

最後,我們的jsp表單提交方式要爲POST,否則會出現但下一步按鈕時又回到第一順位的頁面的情況

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章