下面我们搭建一个spring mvc的基本工程
如何下载spring包,在《新手如何配置spring》一文中已说明
在eclipse中创建 Dynamic web project
整个项目目录结构
首先导入一下几个包到lib文件夹
编写控制器类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
package
com.wzltxm.controller;
import
org.springframework.stereotype.Controller;
import
org.springframework.web.bind.annotation.RequestMapping;
import
org.springframework.web.bind.annotation.RequestMethod;
@Controller
(
"commonController"
)
@RequestMapping
(value=
"common"
)
public
class
CommonController {
@RequestMapping
(value=
"login"
, method=RequestMethod.GET)
public
String loginPage(){
return
"login"
;
}
}
|
然后创建spring-root.xml
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
|
<?
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:context
=
"http://www.springframework.org/schema/context"
xmlns:aop
=
"http://www.springframework.org/schema/aop"
xmlns:mvc
=
"http://www.springframework.org/schema/mvc"
xmlns:p
=
"http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
<
mvc:annotation-driven
/>
<
context:component-scan
base-package
=
"com.wzltxm.controller"
></
context:component-scan
>
<
bean
id
=
"viewResolver"
class
=
"org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix
=
"/WEB-INF/page/"
p:suffix
=
".jsp"
>
</
bean
>
</
beans
>
|
接下来编写 spring-mvc.xml
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
|
<?
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:context
=
"http://www.springframework.org/schema/context"
xmlns:aop
=
"http://www.springframework.org/schema/aop"
xmlns:mvc
=
"http://www.springframework.org/schema/mvc"
xmlns:p
=
"http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
<!-- 开启mvc注解功能 -->
<
mvc:annotation-driven
/>
<!-- 配置自动扫描注解组件,如果配置了此项,可省略上面的annotation-driven -->
<
context:component-scan
base-package
=
"com.wzltxm.controller"
></
context:component-scan
>
<!-- 配置视图解析器 -->
<
bean
id
=
"viewResolver"
class
=
"org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix
=
"/WEB-INF/page/"
p:suffix
=
".jsp"
>
</
bean
>
</
beans
>
|
接下来配置web.xml
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
|
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<
web-app
xmlns
=
"http://java.sun.com/xml/ns/javaee"
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version
=
"3.0"
>
<!-- 配置项目名称 -->
<
display-name
>springWebFirst</
display-name
>
<!-- 配置默认首页 -->
<
welcome-file-list
>
<
welcome-file
>index.html</
welcome-file
>
<
welcome-file
>index.htm</
welcome-file
>
<
welcome-file
>index.jsp</
welcome-file
>
</
welcome-file-list
>
<!-- 配置spring上下文监听器 -->
<
listener
>
<
description
>spring监听器</
description
>
<
listener-class
>org.springframework.web.context.ContextLoaderListener</
listener-class
>
</
listener
>
<
context-param
>
<
description
>spring上下文配置文件</
description
>
<
param-name
>contextConfigLocation</
param-name
>
<
param-value
>classpath:spring-root.xml</
param-value
>
</
context-param
>
<
filter
>
<
description
>字符集过滤器</
description
>
<
filter-name
>encodingFilter</
filter-name
>
<
filter-class
>org.springframework.web.filter.CharacterEncodingFilter</
filter-class
>
<
init-param
>
<
description
>字符编码</
description
>
<
param-name
>encoding</
param-name
>
<
param-value
>UTF-8</
param-value
>
</
init-param
>
<
init-param
>
<
description
>请求返回时也重新编码</
description
>
<
param-name
>forceEncoding</
param-name
>
<
param-value
>true</
param-value
>
</
init-param
>
</
filter
>
<!-- 对所有请求进行编码过滤 -->
<
filter-mapping
>
<
filter-name
>encodingFilter</
filter-name
>
<
url-pattern
>/*</
url-pattern
>
</
filter-mapping
>
<!-- 所有请求交给Dispatcher Servlet进行分发处理 -->
<
servlet
>
<
servlet-name
>dispatcher</
servlet-name
>
<
servlet-class
>org.springframework.web.servlet.DispatcherServlet</
servlet-class
>
<
init-param
>
<
description
>springMVC控制器配置文件</
description
>
<
param-name
>contextConfigLocation</
param-name
>
<
param-value
>classpath:spring-mvc.xml</
param-value
>
</
init-param
>
<!-- 启动程序就加载该Servlet -->
<
load-on-startup
>1</
load-on-startup
>
</
servlet
>
<!-- 对所有请求进行分发处理 -->
<
servlet-mapping
>
<
servlet-name
>dispatcher</
servlet-name
>
<
url-pattern
>/</
url-pattern
>
</
servlet-mapping
>
<
servlet-mapping
>
<
servlet-name
>default</
servlet-name
>
<
url-pattern
>*.html</
url-pattern
>
</
servlet-mapping
>
<!-- session设置为20分钟不操作失效 -->
<
session-config
>
<
session-timeout
>20</
session-timeout
>
</
session-config
>
</
web-app
>
|
然后创建login.jsp文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<%@ page language="java" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<
html
>
<
head
>
<
title
>登录页面</
title
>
</
head
>
<
body
>
<
form
action
=
"/login"
method
=
"POST"
>
<
input
type
=
"text"
id
=
"account"
name
=
"account"
/><
br
/><
br
/>
<
input
type
=
"password"
id
=
"password"
name
=
"password"
/><
br
/><
br
/>
<
input
type
=
"submit"
value
=
"提交"
/>
</
form
>
</
body
>
</
html
>
|
至此配置完成
通过:http://localhost:8080/SpringWebFirst/common/login 便可浏览login.jsp页面
下面我们来解释上面视图解析器的意思
视图解析器的作用是给控制器中返回的login添加前后缀,让它找到web-inf/page/login.jsp这个文件,
因为在web-inf目录下的文件是不可见的,安全性较高,需要通过服务器程序去查找,在浏览器无法通过路径获取