springMvc(初识+操作步骤)
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(初识+操作步骤)的更多相关文章
- ABP创建数据库操作步骤
1 ABP创建数据库操作步骤 1.1 SimpleTaskSystem.Web项目中的Web.config文件修改数据库配置. <add name="Default" pro ...
- Altium Designer PCB双面板制作打印操作步骤
Altium Designer PCB双面板制作打印操作步骤百度知道:http://jingyan.baidu.com/article/335530da83441c19cb41c3db.html?st ...
- yii2 rbac权限控制详细操作步骤
作者:白狼 出处:http://www.manks.top/article/yii2_rbac_description本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出 ...
- yii2权限控制rbac之详细操作步骤
本篇的主题是 rbac权限控制的详细操作步骤,注意是操作步骤哦,关于配置与rbac的搭建,我们在博文 yii2搭建完美后台并实现rbac权限控制实例教程说的再清楚不过了. 但是,在很多人的反馈下,说是 ...
- windows2003最详细的安装操作步骤.(最详细)
以下为windows2003的安装操作步骤,由于安装操作步骤较多,安装可能需要一定的实际安装经验.安装时请参照此文档一步步完成安装. 一.首先准备好Windows2003安装光盘CD1,将CD1光盘放 ...
- selenium高亮显示操作步骤方法
package com.allin.pc;import java.util.List;import org.openqa.selenium.WebElement;import org.openqa.s ...
- ArcGIS生成根据点图层生成等值面并减小栅格锯齿的操作步骤
一.打开ArcMAP并加载上相应的点图层和边界面图层 二.ArcToolbox--Spatial Analyst工具--差值分析--克里金法(根据不同的情况选择不同的算法,这次的处理实际上使用的是样条 ...
- 联想预装win8系统改成win7操作步骤及注意事项
联想消费台式机与一体机预装Windows8改装Windows7的操作步骤及常见问题 前提说明: 目前联想出厂预装Windows 8的台式和一体机使用都是UEFI+GPT硬盘的组合,并且开启了安全启动, ...
- Eclipse中通过Android模拟器调用OpenGL ES2.0函数操作步骤
原文地址: Eclipse中通过Android模拟器调用OpenGL ES2.0函数操作步骤 - 网络资源是无限的 - 博客频道 - CSDN.NET http://blog.csdn.net/fen ...
随机推荐
- XMU 1615 刘备闯三国之三顾茅庐(三) 【欧拉函数+快速幂+欧拉定理】
1615: 刘备闯三国之三顾茅庐(三) Time Limit: 1000 MS Memory Limit: 128 MBSubmit: 45 Solved: 8[Submit][Status][W ...
- YTU 2838: 改错题AB-装置连接
2838: 改错题AB-装置连接 时间限制: 1 Sec 内存限制: 128 MB 提交: 81 解决: 49 题目描述 注:本题只需要提交标记为修改部分之间的代码,请按照C++方式提交. 有AB ...
- BZOJ_3881_[Coci2015]Divljak_AC自动机+dfs序+树状数组
BZOJ_3881_[Coci2015]Divljak_AC自动机+dfs序+树状数组 Description Alice有n个字符串S_1,S_2...S_n,Bob有一个字符串集合T,一开始集合是 ...
- 【USACO 2011】 道路和航线
[题目链接] 点击打开链接 [算法] SPFA + SLF / LLL 优化 [代码] #include<bits/stdc++.h> using namespace std; #defi ...
- vue 子组件 索引 ref
尽管有props和events,但是有时任然需要在javascript中直接访问子组件.为此可是使用ref为子组件指定一个索引ID. <div id="parent"> ...
- char-rnn-tensorflow源码解析及结构流程分析
char-rnn-tensorflow由飞飞弟子karpathy编写,展示了如何用tensorflow来搭建一个基本的RNN(LSTM)网络,并进行基于char的seq2seq进行训练. 数据读取部分 ...
- bzoj3696
3696: 化合物 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 245 Solved: 131[Submit][Status][Discuss] ...
- asp.net MVC 使用Bootstrap 分页
Boostrap分页控件比较美观, 控制器代码:使用仓储模式实现.如果是直接使用DbContext上下文使用更简单. public ActionResult Index(int? page,strin ...
- bzoj 1880: [Sdoi2009]Elaxia的路线【spfa+拓扑排序】
有趣啊 先spfa分别求出以s1,t1,s2,t2为起点的最短路,然后把在s1-->t1或者s2-->t2最短路上的边重新建有向图,跑拓扑最长路即可 #include<iostrea ...
- 关于python安装lxml插件的问题
文章只是介绍自己安装时从安装不上到安装后报错,再到安装成功的心路历程,并不代表广大欧皇也会会出现同类型的问题,也不是总结和汇总各种出问题的原因. 直接进入正题,首先我这边是win环境,电脑上装的是py ...