文章目录
  1. 1. SpringMVC介绍
  2. 2. SpringMVC原理
    1. 2.1. 原理图
  3. 3. 运行原理
  4. 4. 实例

SpringMVC介绍


了解一个东西,其实看它的文档是最快速的学习方法了。Spring4.x的文档也在翻译过程中,目前我找到的是github上的一个翻译小组spring-framework-reference,还在翻译过程中,但关于SpringMVC部分的内容已经翻译的差不多了。英文很强的可以直接看官方的英文文档。我这里就直接摘抄文档中关于SpringMVC的介绍。

Spring Web 模型-视图-控制器(MVC) 框架是围绕 DispatcherServlet而设计的,其支持可配置的 handler 映射,视图解析,本地化、时区和主题的解析以及文件上传的功能。DispatcherServlet 负责将请求分发到不同的 handler。默认的 handler 通过@Controller@RequestMapping注解,提供多种灵活的处理方法。若加上 @PathVariable 注解和其他辅助功能,你也可用使用 @Controller 机制来创建 RESTful web 站点和应用程序。

使用 Spring Web MVC ,你不需要实现框架指定的任何接口或继承任意基类,就可以使用任意对象作为命令对象(或表单对象)。Spring 的数据绑定相当之灵活,比如,Spring可以将不匹配的类型作为应用可识别的验证错误,而不是系统错误,所以,你不需要去重复定义一套属性一致而类型是原始字符串的业务逻辑对象,去处理错误的提交或对字符串进行类型转换。反过来说就是,spring 允许你直接将正确类型的参数绑定到业务逻辑对象。

Spring 的视图解析也相当之灵活。完成一个请求,Controller 通常是负责准备一个数据模型 Map 和选择一个指定的视图,当然,也支持直接将数据写到响应流里。视图名称的解析是高度可配置的,可以通过文件扩展名、accept header 的 Content-Type、bean 的名称、属性文件或自定义的 ViewResolver 实现来解析。模型(Model,MVC 中的 M),是一个 Map 接口,提供对视图数据的完全抽象,可直接与渲染模版集成,如 JSP,Veloctiy,Freemarker;或直接生成原始数据,或xml、json等其他类型的响应内容。模型 Map 接口只是负责将数据转换为合适格式,如 jsp 请求属性,velocity 的 model 等。

SpringMVC原理


原理图

SpringMVC原理图

  • DispatcherServlet:Spring提供的前端控制器,所有的请求都有经过它来统一分发。在DispatcherServlet将请求分发给Spring Controller之前,需要借助于Spring提供的HandlerMapping定位到具体的Controller。
  • HandlerMapping:能够完成客户请求到Controller映射。
  • Controller:需要为并发用户处理上述请求,因此实现Controller接口时,必须保证线程安全并且可重用。Controller将处理用户请求,这和Struts Action扮演的角色是一致的。一旦Controller处理完用户请求,则返回ModelAndView对象给DispatcherServlet前端控制器,ModelAndView中包含了模型(Model)和视图(View)。从宏观角度考虑,DispatcherServlet是整个Web应用的控制器;从微观考虑,Controller是单个Http请求处理过程中的控制器,而ModelAndView是Http请求过程中返回的模型(Model)和视图(View)。
  • ViewResolver:Spring提供的视图解析器(ViewResolver)在Web应用中查找View对象,从而将相应结果渲染给客户。

运行原理

  1. 客户端请求提交到DispatcherServlet
  2. 由DispatcherServlet控制器查询一个或多个HandlerMapping,找到处理请求的Controller
  3. DispatcherServlet将请求提交到Controller
  4. Controller调用业务逻辑处理后,返回ModelAndView
  5. DispatcherServlet查询一个或多个ViewResoler视图解析器,找到ModelAndView指定的视图
  6. 视图负责将结果显示到客户端

实例


项目是要在Eclipse中用Maven构建的。

1、首先在pom.xml中添加所需要的dependency。如下:

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
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.1.6.RELEASE</spring.version>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>

2、在web.xml中配置文件中关于SpringMVC的配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">

<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>/</url-pattern>
</servlet-mapping>
</web-app>

3、在web.xml的同级目录中,添加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
<?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: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">

<!-- scan the package and the sub package -->
<context:component-scan base-package="com.lihui.springmvc" />
<!-- don't handle the static resource -->
<mvc:default-servlet-handler />
<!-- if you use annotation you must configure following setting -->
<mvc:annotation-driven />
<!-- configure the InternalResourceViewResolver -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">

<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- 后缀 -->
<property name="suffix" value=".jsp" />
</bean>
</beans>

4、建立包com.lihui.springmvc.controller以及IndexController

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.lihui.springmvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/index")
public class IndexController {
@RequestMapping(value = { "", "/" }, method = RequestMethod.GET)
public String index() {
return "/index";
}
}

5、在WEB-INF文件夹下创建名为jsp的文件夹,用来存放jsp的视图页面。创建index.jsp文件。

1
2
3
<body>
SpringMVC Hello World
</body>

6、启动服务器,在浏览器中输入http://localhost:8080/SpringMVC-Hello/index

文章目录
  1. 1. SpringMVC介绍
  2. 2. SpringMVC原理
    1. 2.1. 原理图
  3. 3. 运行原理
  4. 4. 实例