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. rrdtool ubuntu python snmpwalk

    rrdtool install: apt-get install libpango1.0-dev libxml2-dev wget https://packages.debian.org/wheezy ...

  2. go学习笔记二:运行使用命令行参数

    本文只作为博主的go语言学习笔记. 对命令行参数的解析,只是在运行时使用的,比如以下命令:go run gomain -conf conf.toml 没有办法再go build时使用. 一.运行时命令 ...

  3. golang 系统包自动填写插件

    Make sure $GOPATH/bin is in your $PATH (Windows: %GOPATH%\bin goes in your %PATH%). [保证你的golang环境正常] ...

  4. Asp.Net MVC以JSON传值扩展方法

    Asp.Net在客户端和服务器端,以JSON形式相互传值,可写扩展方法,用到的类型如下: DataContractJsonSerializer类: 该类在System.Runtime.Serializ ...

  5. 关于/proc/进程idpid/fd ,根据fd来查找连接

    当创建好epoll句柄后,它就是会占用一个fd值,在linux下如果查看/proc/进程id/fd/,是能够看到这个fd的,所以在使用完epoll后,必须调用close()关闭,否则可能导致fd被耗尽 ...

  6. XE6移动开发环境搭建之IOS篇(2):安装虚拟机(有图有真相)

    XE6移动开发环境搭建之IOS篇(2):安装虚拟机(有图有真相) 2014-08-15 22:04 网上能找到的关于Delphi XE系列的移动开发环境的相关文章甚少,本文尽量以详细的内容.傻瓜式的表 ...

  7. XDU 1109

    #include<stdio.h> #define N 10007 #define maxn 1000005 int dp[maxn]; int main() { dp[]=,dp[]=, ...

  8. 接口API中的敏感数据基于AES进行安全加密后返回

    许久没有写博客了,有些惶恐地打开这个再熟悉不过的编辑器. 场景:要对一个涉及到敏感数据(账号.密码)的接口进行加密后返回 由于之前没有相关的经验,所以先在网上搜罗了一阵,这篇博客不错https://w ...

  9. maven 介绍(二)

    本文内容主要摘自:http://www.konghao.org/index 内部视频 三.仓库 仓库:本地仓库:远程仓库:私有仓库(nexus) 1. nexus 的安装: 1). 下载并且解压缩 2 ...

  10. 20145316《Java程序设计》第六周学习总结

    20143516许心远 <Java程序设计>第6周学习总结 教材学习内容总结 10.1.1 1.Java将输入/输出抽象化为串流,数据有来源及目的地,衔接两者的是串流对象. 2.若要将数据 ...