`

org.springframework.web.servlet.mvc.SimpleFormController的简单使用

阅读更多

页面:

<%@ 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 ="提交" onclick="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;
	}
}
 
分享到:
评论

相关推荐

    Spring—mvc实例

    &lt;bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/&gt; &lt;bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/&gt; ...

    Spring的各种控制器

    import org.springframework.web.servlet.ModelAndView; public class SimpleController extends SimpleFormController { @RequestMapping("/example") public ModelAndView handleRequest(HttpServletRequest ...

    在一个form表单里同时上传多个文件和文本信息的解决方案

    import org.springframework.web.servlet.mvc.SimpleFormController; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class...

    spring mvc 附件上传代码

    - `&lt;servlet-class&gt;`指定了servlet类为`org.springframework.web.servlet.DispatcherServlet`。 - `&lt;load-on-startup&gt;1&lt;/load-on-startup&gt;`表示此servlet在服务器启动时加载,优先级为1。 2. **URL映射**: - `...

    SpringMVC教程及实现原理

    - **org.springframework.web.servlet-3.0.5.RELEASE.jar**:提供了一个具体的Web层实现,主要用于构建MVC架构的应用程序。 #### 三、SpringMVC使用步骤 1. **导入JAR包**:首先需要将上述提到的所有JAR包导入到...

    使用 Spring 2.5 基于注解驱动的 Spring MVC.doc

    这与传统的实现 `org.springframework.web.servlet.mvc.Controller` 接口或者继承 `SimpleFormController` 或 `MultiActionController` 不同,`@Controller` 注解使得我们的类变成了一个纯粹的 POJO,无需额外的接口...

    springmvc非注解controller控制器

    import org.springframework.web.servlet.ModelAndView; public class MyNonAnnotatedController implements Controller { @Override public ModelAndView handleRequest(HttpServletRequest request, ...

    spring_mvc注解入门

    在传统的Spring MVC中,Controller通常需要实现`org.springframework.web.servlet.mvc.Controller`接口,或者是继承`SimpleFormController`或`MultiActionController`。但是使用注解后,Controller不再需要继承任何...

    spring mvc 文档

    public class SimpleFormController extends org.springframework.web.servlet.mvc.SimpleFormController { public SimpleFormController() { setCommandClass(User.class); setCommandName("user"); } ...

    SpringMVC实例

    &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt; &lt;param-name&gt;contextConfigLocation &lt;param-value&gt;/WEB-INF/classes/applicationContext.xml &lt;load-on-startup&gt;2 &lt;/...

    详解springMVC—三种控制器controller

    &lt;bean id="toLogin" name="/toLogin.do" class="org.springframework.web.servlet.mvc.ParameterizableViewController"&gt; &lt;!-- 配置所跳转到的视图名称 --&gt; &lt;property name="viewName" value="login"&gt;&lt;/property&gt; ...

Global site tag (gtag.js) - Google Analytics