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>