执行流程如下:
【步骤一】
创建一个MyFormController
欲执行则必须创建一个doSubmitAction方法。
且Post方式提交才会触发doSubmitAction方法,切记切记。
package com.myapps.web.controller;
import org.springframework.web.servlet.mvc.SimpleFormController;
import com.myapps.pojo.Person;
public class MyFormController extends SimpleFormController {
public MyFormController(){
this.setCommandClass(Person.class);
this.setCommandName("person");
}
@Override
protected void doSubmitAction(Object command) throws Exception {
Person p=(Person)command;
System.out.println("我是MyFormController:"+p);
}
}
【步骤二】
在spring-mvc.xml中添加上相应的配置如下:
<!-- SimpleFormController -->
<bean name="/myform.action" class="com.myapps.web.controller.MyFormController">
<property name="formView" value="formController"></property>
<property name="successView" value="index"></property>
</bean>
【步骤三】
在/WEB-INF/jsps/formController.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Test FormController</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/myform.action"
method="post">
<input type="hidden" name="id">
<table>
<tr>
<td>name:</td>
<td><input type="text" name="name" />
</td>
</tr>
<tr>
<td>age:</td>
<td><input type="text" name="age" />
</td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="提交表单" />
</td>
</tr>
</table>
</form>
</body>
</html>
【步骤四】
在浏览器地址栏输入:
http://localhost:8080/springmvc/myform.action
控制台打印输出:
我是MyFormController:Person [name=luffy, age=17]