页面:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>org.springframework.web.servlet.mvc.SimpleFormController简单使用</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">
<script type="text/javascript">
function mySubmit() {
document.submitform.submit();
}
</script>
</head>
<body>
<div align="center" >
<form action="onsubmit.do" method="post" name="submitform">
<div>姓名:<input type="text" name="name" value ="" /></div>
<div>年龄:<input type="text" name="age" value ="" /></div>
<div><input type="button" value ="提交" οnclick="mySubmit();"/></div>
</form>
</div>
</body>
</html>
java服务端:
package com.july;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import domain.Person;
public class OnSubmitController extends SimpleFormController{
protected String success;
protected String fail;
public OnSubmitController() {
setCommandClass(Person.class);//设置转换的类,Person.class为自定义的一个实体类
}
@Override
protected ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors)
throws Exception {
System.out.println("execute ......................................");
Person person = (Person) command;
System.out.println(person);
String result = "姓名:"+person.getName()+",年龄:"+person.getAge();
Map map = new HashMap();
map.put("result", result);
return new ModelAndView(this.getSuccess(),map);
}
public String getSuccess() {
return success;
}
public void setSuccess(String success) {
this.success = success;
}
public String getFail() {
return fail;
}
public void setFail(String fail) {
this.fail = fail;
}
}