1.导入包
2.配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
1
2
3
4
5
6
7
8
9
10
11
12
3.创建springmvc的核心配置文件
文件的命名规则:中央控制器(servlet的名称)的名称+“-servlet.xml”
默认位置:WEB-INF下

springmvc的头信息

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">
</beans>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
4.写controller

@controller:标识当前类是控制层的一个具体的实现
@requestMapping:放在方法上面用来指定某个方法的路径,当它放在类上的时候相当于命名空间需要组合方法上的requestmapping来访问。

@Controller
@RequestMapping("/test")
public class TestController {

@RequestMapping("/hello.do")
public String hello() {

return "index";
}

@RequestMapping("/reparam.do")
public String reparam(HttpServletRequest req) {
String name=req.getParameter("name");
System.out.println(name);
return "index";
}

/*
* 底层走的就是HttpServletRequest
*/
@RequestMapping("/re2.do")
public String re2(String name) {

System.out.println(name);
return "index";

}

@RequestMapping("/re3.do")
public String re3(String name,Integer id,Date bir) {

System.out.println(name+" "+id+" "+bir);
return "index";

}

@InitBinder
public void initBind(ServletRequestDataBinder binder) {

binder.registerCustomEditor(Date.class, new CustomDateEditor(
new SimpleDateFormat("yyyy-MM-dd"), true));
}

@RequestMapping("/toform.do")
public String toform() {

return "form";
}

@RequestMapping("/form.do")
public String form(String[] favor) {

for(String fa :favor)
System.out.println(fa);

return "index";
}

@RequestMapping("/toperson.do")
public String toperson(Person person) {

System.out.println(person);
return "success";
}

//参数map是往页面写的
@RequestMapping("/getP.do")
public String getP(Map<String, Object> map) {
Person p1=new Person(2, "lisi", "bj", 2, new Date());
map.put("person", p1);
return "returnPage";
}

//最常用的
@RequestMapping("/getM.do")
public String getM(Model model) {
Person p1=new Person(2, "lisi", "bj", 2, new Date());
model.addAttribute("person", p1);
return "returnPage";
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
1.在方法中可以自己随意去定义方法的参数,如果方法的参数的名称与传入参数的name匹配就会自动接收,别且转换我们所定义的数据类型。如果参数列表里定义了自定义的类springmvc会给我们把匹配的参数手机起来并且组装成对象。
2. requestMapping里面的method的类型必须要与前台form的类型一致

//-------------------------------------------------------------------------------------------------------

5.视图解析器(写在3的<beans></beans>里面)

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"> </property>
<property name="suffix" value=".jsp"></property>
</bean>
1
2
3
4
6.在springmvc的配置文件中指定注解驱动,配置扫描器
(有扫描器就不用开启驱动了)

<context:component-scan base-package="com.rl.controller"></context:component-scan>

springMvc(初识+操作步骤)的更多相关文章

  1. ABP创建数据库操作步骤

    1 ABP创建数据库操作步骤 1.1 SimpleTaskSystem.Web项目中的Web.config文件修改数据库配置. <add name="Default" pro ...

  2. Altium Designer PCB双面板制作打印操作步骤

    Altium Designer PCB双面板制作打印操作步骤百度知道:http://jingyan.baidu.com/article/335530da83441c19cb41c3db.html?st ...

  3. yii2 rbac权限控制详细操作步骤

    作者:白狼 出处:http://www.manks.top/article/yii2_rbac_description本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出 ...

  4. yii2权限控制rbac之详细操作步骤

    本篇的主题是 rbac权限控制的详细操作步骤,注意是操作步骤哦,关于配置与rbac的搭建,我们在博文 yii2搭建完美后台并实现rbac权限控制实例教程说的再清楚不过了. 但是,在很多人的反馈下,说是 ...

  5. windows2003最详细的安装操作步骤.(最详细)

    以下为windows2003的安装操作步骤,由于安装操作步骤较多,安装可能需要一定的实际安装经验.安装时请参照此文档一步步完成安装. 一.首先准备好Windows2003安装光盘CD1,将CD1光盘放 ...

  6. selenium高亮显示操作步骤方法

    package com.allin.pc;import java.util.List;import org.openqa.selenium.WebElement;import org.openqa.s ...

  7. ArcGIS生成根据点图层生成等值面并减小栅格锯齿的操作步骤

    一.打开ArcMAP并加载上相应的点图层和边界面图层 二.ArcToolbox--Spatial Analyst工具--差值分析--克里金法(根据不同的情况选择不同的算法,这次的处理实际上使用的是样条 ...

  8. 联想预装win8系统改成win7操作步骤及注意事项

    联想消费台式机与一体机预装Windows8改装Windows7的操作步骤及常见问题 前提说明: 目前联想出厂预装Windows 8的台式和一体机使用都是UEFI+GPT硬盘的组合,并且开启了安全启动, ...

  9. Eclipse中通过Android模拟器调用OpenGL ES2.0函数操作步骤

    原文地址: Eclipse中通过Android模拟器调用OpenGL ES2.0函数操作步骤 - 网络资源是无限的 - 博客频道 - CSDN.NET http://blog.csdn.net/fen ...

随机推荐

  1. Ubuntu 12.10终端Terminal快捷方式调用

    1:使用快捷键:ctrl+alt+t 打开终端 2:在终端上右键,选“Lock to launcher” 这样就锁定在左侧了,需要用时,直接点就打开了.

  2. YTU 2811: 打鱼还是晒网

    2811: 打鱼还是晒网 时间限制: 1 Sec  内存限制: 128 MB 提交: 192  解决: 150 题目描述 中国有句俗话"三天打鱼,两天晒网".小王从2000年的1月 ...

  3. svn问题:在eclipse里面使用SVN,怎么实现版本回滚呢?

    共有4个答案 我要回答» JustForFly 回答于 2012-04-27 10:20 举报   想回到SVN服务器端的最新版本就使用 team->还原.. 想回到SVN服务器端的其它版本使用 ...

  4. Linux 打包classes

    进入项目后 tar -zcvf ~/${HOSTNAME}-${PWD##*/}-$(date +%Y-%m-%d)-classes.tar.gz webapps/ROOT/WEB-INF/class ...

  5. 并不对劲的bzoj4199: [Noi2015]品酒大会

    传送门-> 又称普及大会. 这题没什么好说的……后缀自动机裸题……并不对劲的人太菜了,之前照着标程逐行比对才过了这道题,前几天刚刚把这题一遍写对…… 这题的输出和某两点相同后缀的长度有关,那么把 ...

  6. 【转载】Nginx 的工作原理 和优化

    1. Nginx的模块与工作原理 Nginx由内核和模块组成,其中,内核的设计非常微小和简洁,完成的工作也非常简单,仅仅通过查找配置文件将客户端请求映射到一个location block(locati ...

  7. 洛谷 P3621 [APIO2007]风铃【贪心】

    没有算法,但是要注意细节. 首先无解的情况,显然的是最小深度的叶子节点和最大深度的叶子节点的深度差大于1:还有一种比较难想,就是如果一个点的左右子树都有最大和最小深度的叶子节点,这样交换左右子树也不行 ...

  8. codeforces 1006 F(折半搜索)

    F. Xor-Paths time limit per test 3 seconds memory limit per test 256 megabytes input standard input ...

  9. 关于自增id 你可能还不知道

    导读:在使用MySQL建表时,我们通常会创建一个自增字段(AUTO_INCREMENT),并以此字段作为主键.本篇文章将以问答的形式讲述关于自增id的一切. 注: 本文所讲的都是基于Innodb存储引 ...

  10. js实现打字效果

    <!DOCTYPE html> <html> <head> <meta charset='utf-8'> <title>js typing& ...