页面:
<%@ 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;
}
}
分享到:
相关推荐
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> ...
import org.springframework.web.servlet.ModelAndView; public class SimpleController extends SimpleFormController { @RequestMapping("/example") public ModelAndView handleRequest(HttpServletRequest ...
import org.springframework.web.servlet.mvc.SimpleFormController; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class...
- `<servlet-class>`指定了servlet类为`org.springframework.web.servlet.DispatcherServlet`。 - `<load-on-startup>1</load-on-startup>`表示此servlet在服务器启动时加载,优先级为1。 2. **URL映射**: - `...
- **org.springframework.web.servlet-3.0.5.RELEASE.jar**:提供了一个具体的Web层实现,主要用于构建MVC架构的应用程序。 #### 三、SpringMVC使用步骤 1. **导入JAR包**:首先需要将上述提到的所有JAR包导入到...
这与传统的实现 `org.springframework.web.servlet.mvc.Controller` 接口或者继承 `SimpleFormController` 或 `MultiActionController` 不同,`@Controller` 注解使得我们的类变成了一个纯粹的 POJO,无需额外的接口...
import org.springframework.web.servlet.ModelAndView; public class MyNonAnnotatedController implements Controller { @Override public ModelAndView handleRequest(HttpServletRequest request, ...
在传统的Spring MVC中,Controller通常需要实现`org.springframework.web.servlet.mvc.Controller`接口,或者是继承`SimpleFormController`或`MultiActionController`。但是使用注解后,Controller不再需要继承任何...
public class SimpleFormController extends org.springframework.web.servlet.mvc.SimpleFormController { public SimpleFormController() { setCommandClass(User.class); setCommandName("user"); } ...
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <param-name>contextConfigLocation <param-value>/WEB-INF/classes/applicationContext.xml <load-on-startup>2 </...
<bean id="toLogin" name="/toLogin.do" class="org.springframework.web.servlet.mvc.ParameterizableViewController"> <!-- 配置所跳转到的视图名称 --> <property name="viewName" value="login"></property> ...