首页 > 代码库 > 使用Spring MVC 的表单控制器SimpleFormController

使用Spring MVC 的表单控制器SimpleFormController

以注册过程为例,我们可能会选择继承AbstractController来实现表单的显示,继承AbstractCommandController来实现表单的处理 ,这样是可行的,但必须要维护两个控制器

在这种情况下,我们应该使用SimpleFormController,他接受GET请求时显示表单,接受POST请求时处理表单,如果发生错误,控制器会知道重新显示这个表单,这样用户就可以修改错误,重新提交。

表单对应的POJO

package com.dxz.validator.demo1.mode;public class Student {    private String name;    private String sex;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getSex() {        return sex;    }    public void setSex(String sex) {        this.sex = sex;    }}

控制器:

这个base类中还有一个DoSubmitAction()方法,和onSubmit()方法的区别就是后者可以返回一个ModelAndView对象,完成向页面输出数据的功能,而前者不能向页面返回数据,这两个方法同时只有一个有效。

package com.dxz.validator.demo1.action;import org.springframework.validation.BindException;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.SimpleFormController;import com.dxz.validator.demo1.mode.Student;public class RegisterStudentController extends SimpleFormController {    public RegisterStudentController() {        this.setCommandClass(Student.class);    }    protected ModelAndView onSubmit(Object object, BindException arg1)            throws Exception {        Student stu = (Student) object;        return new ModelAndView(getSuccessView(), "student", stu);    }}

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-service.xml,/WEB-INF/train-data.xml,/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>    <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>

train-servlet.xml

formView定义为register对应我们的表单提交页面register.jsp
successView定义为success对应提交成功的显示页面success.jsp

<bean id="RegisterStudentController" class="Action.RegisterStudentController">  <property name="formView">    <value>register</value>  </property>  <property name="successView">    <value>success</value>  </property></bean><bean id="simpleUrlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings">   <props>     <prop key="/home.mvc">HomeController</prop>     <prop key="/register.mvc">RegisterStudentController</prop>   </props> </property></bean>

register.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="http://www.mamicode.com/styles.css">    -->  </head>    <body>   <form action="<%=request.getContextPath() %>/register.mvc" method="post">     name:<input type="text" name="name"/></br>     sex:<input type="text" name="sex"/></br>     <input type="submit" value="submit"/>        </form>  </body></html>

success.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="http://www.mamicode.com/styles.css">    -->  </head>    <body>    ${student.name}-----${student.sex} <br>  </body></html>

测试运行,可以看到success.jsp上有我们提交的信息

去-----1  (使用filter处理中文问题)