SpringMVC封装表单数据
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封装表单数据的更多相关文章
- 框架学习之Struts2(二)---基本配置和封装表单数据
一.结果页面配置 1.局部结果页面配置 <!-- 局部结果页面配置--> <package name = "demo" extends = "strut ...
- 利用BeanUtils工具类封装表单数据
一.BeanUtils工具类的使用 1.首先导入BeanUtils工具类的jar包 commons-beanutils-1.8.0.jar commons-logging-1.1.1.jar 2.se ...
- httpclient模拟post请求json封装表单数据
好长时间不更博了,主要肚子里没什么好墨水,哈哈.废话不说上代码. public static String httpPostWithJSON(String url) throws Exception ...
- request对象多种方法封装表单数据
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, ...
- SpringMVC接受表单数据
@ 目录 pojo addProduct.jsp ProductController showProduct.jsp 测试结果 pojo 新建实体类Product package pojo; publ ...
- JavaWeb - 模仿SpringMVC抽取 BaseServlet + 封装表单参数
模仿SpringMVC抽取一个BaseServlet,接收所有请求,然后自动封装表单参数和分发到对应的servlet执行,下面用一个页面表单提交,转发显示的项目做示例. 1)首先准备一个Entity, ...
- Servlet的5种方式实现表单提交(注册小功能),后台获取表单数据
用servlet实现一个注册的小功能 ,后台获取数据. 注册页面: 注册页面代码 : <!DOCTYPE html> <html> <head> <meta ...
- 【ASP.NET Web API教程】5.2 发送HTML表单数据:URL编码的表单数据
原文:[ASP.NET Web API教程]5.2 发送HTML表单数据:URL编码的表单数据 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本系列教程,请先看前面的内 ...
- springMVC(7)---表单标签
springMVC(7)---表单标签 form标签作用 简单来讲form表单有两大作用 1:第一就是往后端提交数据或者前端回显 ...
随机推荐
- Storm-源码分析-Topology Submit-Nimbus-mk-assignments
什么是"mk-assignment", 主要就是产生executor->node+port关系, 将executor分配到哪个node的哪个slot上(port代表slot, ...
- Mustache 中的html转义问题处理
避免在使用Mustache引擎是发生html字符转义 1,模板代码示例: var xml= " <?xml version="1.0" encoding=&q ...
- IIs7下配置php
因为一个朋友的服务器是window的需要两个版本的php,一个是现在用的php5.2,现在要用一个5.3的版本,所以考虑IIS下的配置. 1.首先当然是要下载一份php了,我采用的是免安装的,很方便, ...
- 【云安全与同态加密_调研分析(2)】国外云安全标准建议组织——By Me
国际上比较有影响力的云安全组织: ◆2. 国外云安全标准建议组织(云安全建议白皮书)◆ ◆云安全标准建议组织(主要的)◆ ◆标准建议组织介绍◆ ◆相关建议白皮书制定◆ ◆建立的相关模型参考◆ ◆备注( ...
- 001-hive是什么
一.基本概念 官网含义:https://cwiki.apache.org/confluence/display/Hive/Home The Apache Hive™ data warehouse so ...
- 汉字转换为拼音的JavaScript库
将JSPinyin剥离mootools这个JavaScript库,可以独立使用. 1)一个是将汉字翻译为拼音,其中每一个字的首字母大写: pinyin.getFullChars(this.value) ...
- IntBuffer类的基本用法
package com.ietree.basicskill.socket.basic.nio; import java.nio.IntBuffer; /** * Created by Administ ...
- oracle dataguard参数
在整个dg配置中,最复杂的也许就是参数的配置了,并且有许多参数都可以延伸出去讲很多,所以今天我们来看看dg的参数配置,顺便加上一点dataguard进程相关的信息,帮助理解. 在配置dg的过程中,我们 ...
- php.ini优化,,,php-fpm
无论是apache还是nginx,php.ini都是合适的.而php-fpm.conf适合nginx+fcgi的配置. 1)打开PHP的安全模式 PHP的安全模式是个非常重要的PHP内嵌的安全机制,能 ...
- SQL Server outer apply 和 cross apply
先说点题外话,因为后面我会用到这个函数. 前两天自定义了一个 sql 的字符串分割函数(Split),不过后来发现有点问题,例如: select * from Split(default,'123,4 ...