如果我们的表单有很多的输入选项,如果都放置在一个页面上,会导致页面过多,如果我们能把这些输入分散到几个页面上,按向导的方式填写,在最后一页上进行提交,势必会带来良好的可操作性,使用Spring MVC 架构中的AbstractWizardFormController控制器,可以很轻松的完成以上功能
(1)配置文件:web.xml
< 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方法不是必须实现的,他是在你填写某一步表单时想取消,按取消按钮时调用。
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:
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;
}
}
配置文件:
< 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:
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 >