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,否则会出现但下一步按钮时又回到第一顺位的页面的情况

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值