一 创建一个动态web项目,并导入jar包
导入最新的spring的jar包,可以自己在官网下载或者导入我用的jar包,链接:http://pan.baidu.com/s/1mhfQ9h2 导入之后是这样的:
二 配置web.xml
文件路径是:WebContent/WEB-INF/web.xml ,代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?><
web-app
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
xmlns
=
"http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation
=
"http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version
=
"3.1"
>
<
servlet
>
<
servlet-name
>SpringMVC</
servlet-name
>
<
servlet-class
>org.springframework.web.servlet.DispatcherServlet</
servlet-class
>
<
load-on-startup
>1</
load-on-startup
>
</
servlet
>
<
servlet-mapping
>
<
servlet-name
>SpringMVC</
servlet-name
>
<
url-pattern
>/</
url-pattern
>
</
servlet-mapping
>
<
welcome-file-list
>
<
welcome-file
>index.jsp</
welcome-file
>
</
welcome-file-list
></
web-app
>
|
(1)load-on-startup:表示启动容器时初始化该Servlet
(2)url-pattern:表示哪些请求交给Spring Web MVC处理, “/” 是用来定义默认servlet映射的
(3)Dispatcherservlet:默认DispatcherServlet会加载WEB-INF/[DispatcherServlet的Servlet名字]-servlet.xml配置文件,在本例中是:WEB-INF/SpringMVC-servlet.xml
注:SpringMVC-servlet.xml文件的路径可以自定义,比如说放在src目录下,这时候需要修改web.xml文件为:
1
2
3
4
5
6
7
8
9
|
<
servlet
>
<
servlet-name
>SpringMVC</
servlet-name
>
<
servlet-class
>org.springframework.web.servlet.DispatcherServlet</
servlet-class
>
<
init-param
>
<
param-name
>contextConfigLocation</
param-name
>
<
param-value
>classpath:SpringMVC-servlet.xml</
param-value
>
</
init-param
>
<
load-on-startup
>1</
load-on-startup
>
</
servlet
>
|
三 配置SpringMVC-servlet.xml
在这里,这个文件跟web.xml在同一目录下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<?
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:p
=
"http://www.springframework.org/schema/p"
xmlns:context
=
"http://www.springframework.org/schema/context"
xmlns:mvc
=
"http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
<
bean
name
=
"/test1/hello"
class
=
"cn.zifangsky.ControllerDemo_1"
></
bean
>
<!-- ViewResolver -->
<
bean
id
=
"viewResolver"
class
=
"org.springframework.web.servlet.view.InternalResourceViewResolver"
>
<
property
name
=
"prefix"
value
=
"/WEB-INF/jsp/"
/>
<
property
name
=
"suffix"
value
=
".jsp"
/>
</
bean
>
</
beans
>
|
(1)首先定义了一个名为”/test1/hello”的action,对应的处理类是:cn.zifangsky.ControllerDemo_1。启动项目后访问:http://localhost:8080/项目名/test1/hello 即可转入ControllerDemo_1这个处理类
(2)InternalResourceViewResolver:视图名称解析器。在这里prefix和suffix分别表示查找视图页面的前缀和后缀,比如传进来的逻辑视图名为hello,那么该jsp视图页面应该存放在”/WEB-INF/jsp/hello.jsp”
四 action处理类ControllerDemo_1
从上面的SpringMVC-servlet.xml可以看出,ControllerDemo_1存放在cn.zifangsky.ControllerDemo_1,在这里我就简单的打印一个字符串,并返回一个名叫”index“的逻辑视图
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
package
cn.zifangsky;
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
ControllerDemo_1
implements
Controller{
public
ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1)
throws
Exception {
System.out.println(
"-------www.zifangsky.cn------"
);
return
new
ModelAndView(
"index"
);
}
}
|
五 视图index.jsp
从SpringMVC-servlet.xml定义的视图前缀和后缀可以得知,返回的视图路径是:[前缀]/逻辑视图[后缀],在这里就是:/WEB-INF/jsp/index.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<
html
>
<
head
>
<
meta
http-equiv
=
"Content-Type"
content
=
"text/html; charset=UTF-8"
>
<
base
href="<%=basePath%>">
<
title
>Welcome to Hear</
title
>
</
head
>
<
body
>
<
div
align
=
"center"
>
This is first page
</
div
>
</
body
>
</
html
>
|
六 运行效果如下
七 采用注解的方式处理action:
(1)修改SpringMVC-servlet.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
|
<?
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:p
=
"http://www.springframework.org/schema/p"
xmlns:context
=
"http://www.springframework.org/schema/context"
xmlns:mvc
=
"http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
<!-- 启用spring mvc 注解 -->
<
mvc:annotation-driven
/>
<!-- 不操作静态资源 -->
<
mvc:default-servlet-handler
/>
<!-- 设置使用注解的类所在的包 -->
<
context:component-scan
base-package
=
"cn.zifangsky"
/>
<!-- <bean name="/test1/hello" class="cn.zifangsky.ControllerDemo_1"></bean> -->
<!-- ViewResolver -->
<
bean
id
=
"viewResolver"
class
=
"org.springframework.web.servlet.view.InternalResourceViewResolver"
>
<
property
name
=
"prefix"
value
=
"/WEB-INF/jsp/"
/>
<
property
name
=
"suffix"
value
=
".jsp"
/>
</
bean
>
</
beans
>
|
(2)编写action:
路径是:cn.zifangsky.ControllerDemo_2,完整代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
package
cn.zifangsky;
import
org.springframework.stereotype.Controller;
import
org.springframework.web.bind.annotation.RequestMapping;
/**
* @Controller 负责注册一个bean 到spring 上下文中
* @RequestMapping 注解为控制器指定可以处理哪些 URL 请求
*
* */
@Controller
@RequestMapping
(
"/test2"
)
public
class
ControllerDemo_2 {
@RequestMapping
(
"/hello"
)
public
String hello() {
return
"hello"
;
}
}
|
注:@Controller 负责注册一个bean 到spring 上下文中;@RequestMapping 注解为控制器指定可以处理哪些 URL 请求。也就是说启动项目后,通过访问:http://localhost:8080/SpringMVCDemo/test2/hello这个访问到这个action
(3)/WEB-INF/jsp/hello.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<
html
>
<
head
>
<
meta
http-equiv
=
"Content-Type"
content
=
"text/html; charset=UTF-8"
>
<
base
href="<%=basePath%>">
<
title
>hello world</
title
>
</
head
>
<
body
>
<
div
align
=
"center"
>
This is second page
</
div
>
</
body
>
</
html
>
|
(4)运行效果如下:
附:SpringMVC常用注解
@Controller 负责注册一个bean 到spring 上下文中
@RequestMapping 注解为控制器指定可以处理哪些 URL 请求
@RequestBody 该注解用于读取Request请求的body部分数据,使用系统默认配置的HttpMessageConverter进行解析,然后把相应的数据绑定到要返回的对象上 ,再把HttpMessageConverter返回的对象数据绑定到 controller中方法的参数上
@ResponseBody 该注解用于将Controller的方法返回的对象,通过适当的HttpMessageConverter转换为指定格式后,写入到Response对象的body数据区
@ModelAttribute 在方法定义上使用 @ModelAttribute 注解:Spring MVC 在调用目标处理方法前,会先逐个调用在方法级上标注了@ModelAttribute 的方法 在方法的入参前使用 @ModelAttribute 注解:可以从隐含对象中获取隐含的模型数据中获取对象,再将请求参数 –绑定到对象中,再传入入参将方法入参对象添加到模型中
@RequestParam 在处理方法入参处使用 @RequestParam 可以把请求参 数传递给请求方法
@PathVariable 绑定 URL 占位符到入参
@ExceptionHandler 注解到方法上,出现异常时会执行该方法
@ControllerAdvice 使一个Contoller成为全局的异常处理类,类中用@ExceptionHandler方法注解的方法可以处理所有Controller发生的异常