1、domain类

 package com.xiaostudy.domain;

 public class User {

     private int id;
private String username;
private String password; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} @Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password=" + password + "]";
} }

2、填写表单数据的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>springMVC</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/command.do" method="post">
ID:<input type="text" name="id" id="id"/>
姓名:<input type="text" name="username" id="username"/>
密码:<input type="password" name="password" id="password"/>
<input type="submit" value="提交"/>
</form>
</body>
</html>

3、springmvc.xml

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"> <!-- ======================================================================================================= -->
<!-- 配置处理器映射器,springmvc默认的处理器映射器
BeanNameUrlHandlerMapping:根据bean(自定义Controler)的name属性的url去寻找hanler(Action:Controller) -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> <bean name="/toadd.do" class="com.xiaostudy.ToAddController"/> <bean name="/command.do" class="com.xiaostudy.CommandController"/> <!-- 配置sprigmvc视图解析器:解析逻辑试图
后台返回逻辑试图:index
视图解析器解析出真正物理视图:前缀+逻辑试图+后缀====/WEB-INF/index.jsp -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>

4、负责跳转到add.jsp的类

 package com.xiaostudy;

 import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller; public class ToAddController implements Controller{ public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {
ModelAndView mv = new ModelAndView();
//调转到add添加页面视图
mv.setViewName("add");
return mv;
} }

5、处理表单数据类

 package com.xiaostudy;

 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.AbstractCommandController; import com.xiaostudy.domain.User; public class CommandController extends AbstractCommandController { public CommandController() {
this.setCommandClass(User.class);
} @Override
protected ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object command, BindException arg3)
throws Exception {
User user = (User)command;
ModelAndView mv = new ModelAndView();
mv.addObject("user", user);
mv.setViewName("index");
return mv;
} }

6、返回到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>springMVC_demo</title>
</head>
<body>
${user.id }||${user.username }||${user.password }
</body>
</html>

项目文件路径


分析执行过程

1、浏览器通过地址访问,经过web.xml拦截,交给springmvc.xml

2、springmvc.xml通过匹配,找到相应的类

3、类返回一个逻辑视图

4、springmvc.xml解析逻辑视图,得到物理视图路径

5、填写表单数据并提交数据

6、web.xml拦截,交给springmvc.xml去处理,springmvc.xml通过匹配,找到相应的类

7、类处理表单数据,并返回一个逻辑视图

8、springmvc.xml解析逻辑视图,得到物理视图路径

9、把数据返回给用户


SpringMVC封装表单数据的更多相关文章

  1. 框架学习之Struts2(二)---基本配置和封装表单数据

    一.结果页面配置 1.局部结果页面配置 <!-- 局部结果页面配置--> <package name = "demo" extends = "strut ...

  2. 利用BeanUtils工具类封装表单数据

    一.BeanUtils工具类的使用 1.首先导入BeanUtils工具类的jar包 commons-beanutils-1.8.0.jar commons-logging-1.1.1.jar 2.se ...

  3. httpclient模拟post请求json封装表单数据

    好长时间不更博了,主要肚子里没什么好墨水,哈哈.废话不说上代码. public static String httpPostWithJSON(String url) throws Exception ...

  4. request对象多种方法封装表单数据

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, ...

  5. SpringMVC接受表单数据

    @ 目录 pojo addProduct.jsp ProductController showProduct.jsp 测试结果 pojo 新建实体类Product package pojo; publ ...

  6. JavaWeb - 模仿SpringMVC抽取 BaseServlet + 封装表单参数

    模仿SpringMVC抽取一个BaseServlet,接收所有请求,然后自动封装表单参数和分发到对应的servlet执行,下面用一个页面表单提交,转发显示的项目做示例. 1)首先准备一个Entity, ...

  7. Servlet的5种方式实现表单提交(注册小功能),后台获取表单数据

    用servlet实现一个注册的小功能 ,后台获取数据. 注册页面: 注册页面代码 : <!DOCTYPE html> <html> <head> <meta ...

  8. 【ASP.NET Web API教程】5.2 发送HTML表单数据:URL编码的表单数据

    原文:[ASP.NET Web API教程]5.2 发送HTML表单数据:URL编码的表单数据 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本系列教程,请先看前面的内 ...

  9. springMVC(7)---表单标签

    springMVC(7)---表单标签 form标签作用                                     简单来讲form表单有两大作用 1:第一就是往后端提交数据或者前端回显 ...

随机推荐

  1. trust an HTTPS connection

    https://zh.wikipedia.org/wiki/传输安全协议 SSL协议客户端要收发几个握手信号: 发送一个“ClientHello”消息,内容包括:支持的协议版本,比如TLS1.0版,一 ...

  2. HDFS集群启动start-dfs.sh报错

    [root@master sbin]# start-dfs.sh Starting namenodes on [master] master: Error: JAVA_HOME is not set ...

  3. js小数四舍五入,保留两位小数

    直接用用number.toFixed(2)即可 <template> <section class="p-10"> <h1> {{ number ...

  4. Python 有什么奇技淫巧?

    知乎上有一个问题:Python 有什么奇技淫巧?其中有各种不按套路出牌的招数,也不乏一些惊为天人的"奇技淫巧",会让你大呼:居然还有这种操作??? 本文就是对日常使用过的或者觉得很 ...

  5. (2.4)备份与还原--WAL与备份原理

    预写式日志(Write-Ahead Logging (WAL))  部分转自:http://www.cnblogs.com/wenBlog/p/4423497.html SQL Server中使用了W ...

  6. select 自动选择 检查下拉列表

    下面我们来看一下selenium webdriver是如何来处理select下拉框的,以Apple注册页面为例. https://appleid.apple.com/cgi-bin/WebObject ...

  7. 在Windows下使用Dev-C++开发基于pthread.h的多线程程序【转】

    在Windows下使用Dev-C++开发基于pthread.h的多线程程序[转]     在Windows下使用Dev-C++开发基于pthread.h的多线程程序   文章分类:C++编程     ...

  8. Jquery each循环用法小结

    var str = res.ZhaoPian; var piclist = str.substring(0, str.length - 1).split(','); $.each(piclist, f ...

  9. pandas--对axis=0,axis=1的理解

    Stackoverflow.com是程序员的好去处,本公众号将以pandas为主题,开始一个系列,争取做到每周一篇,翻译并帮助pandas学习者一起理解一些有代表性的案例.今天的主题就是Pandas与 ...

  10. Tomcat内部结构及请求原理(转)

    Tomcat Tomcat是一个JSP/Servlet容器.其作为Servlet容器,有三种工作模式:独立的Servlet容器.进程内的Servlet容器和进程外的Servlet容器. Tomcat的 ...