Spring MVC 使用介绍(二)—— DispatcherServlet
一、Hello World示例
1、引入依赖
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
2、web.xml中定义servlet
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
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">
<display-name>Archetype Created Web Application</display-name> <servlet>
<servlet-name>test-servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/test-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>test-servlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
3、配置servlet
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd"> <!-- HandlerMapping -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> <!-- HandlerAdapter -->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/> <!-- ViewResolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean> <!-- 处理器 -->
<bean name="/hello" class="cn.matt.controller.TestController"/>
</beans>
配置说明:
- BeanNameUrlHandlerMapping:表示将请求的URL映射为Bean名,如URL为 “上下文/hello”,则Spring配置文件必须有一个名字为“/hello”的Bean
- SimpleControllerHandlerAdapter:表示所有实现了org.springframework.web.servlet.mvc.Controller接口的Bean可以作为Spring Web MVC中的处理器
- InternalResourceViewResolver:用于支持Servlet、JSP视图解析
- viewClass:JstlView表示JSP模板页面需要使用JSTL标签库,classpath中必须包含jstl的相关jar包
- prefix、suffix:查找视图页面的前缀和后缀(前缀[逻辑视图名]后缀),比如传进来的逻辑视图名为hello,则该jsp视图页面应该存放在“WEB-INF/jsp/hello.jsp”
4、处理器
public class TestController implements Controller {
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
//1、收集参数、验证参数
//2、绑定参数到命令对象
//3、将命令对象传入业务对象进行业务处理
//4、选择下一个页面
ModelAndView mv = new ModelAndView();
//添加模型数据 可以是任意的POJO对象
mv.addObject("message", "Hello World!");
//设置逻辑视图名,视图解析器会根据该名字解析到具体的视图页面
mv.setViewName("hello");
return mv;
}
}
5、视图hello.jsp
<%@ page language="java" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
${message}
</body>
</html>
使用tomcat启动,在浏览器输入 http://localhost:8080/myweb/hello 即可访问
二、DispatcherServlet详解
1、DispatcherServlet在web.xml中的配置
<servlet>
<servlet-name>test-servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/test-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>test-servlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
load-on-startup:表示启动容器时初始化该Servlet
url-pattern:表示哪些请求交给Spring Web MVC处理, “/” 是用来定义默认servlet映射的,详细可参考javaweb学习总结(五)——Servlet开发(一)
DispatcherServlet初始化配置:
- contextClass:实现WebApplicationContext接口的类, 默认使用XmlWebApplicationContext
- contextConfigLocation:指定上下文配置文件,可以被分成多个字符串(使用逗号作为分隔符) 来支持多个上下文,默认是“/WEB-INF/[servlet名字]-servlet.xml”。支持相对路径和类路径,如‘classpath:spring-mvc.xml’或‘/WEB-INF/test-servlet.xml’
- namespace:WebApplicationContext命名空间,默认值是[server-name]-servlet
2、通用上下文配置
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
通用上下文用于加载除Web层的其他Bean,如service、dao等,以便与其他Web框架集成
ContextLoaderListener初始化配置:
- contextConfigLocation:指定通用上下文配置文件,使用方式与DispatcherServlet相同,默认会去 /WEB-INFO/ 下加载applicationContext.xml
- contextClass:指定ApplicationContext的实现类,默认为XmlWebApplicationContext
3、DispatcherServlet上下文与通用上下文的关系

通用上下文:用于加载除Web层的其他Bean,如service、dao等,以便与其他Web框架集成;创建完毕后会将该上下文放在ServletContext:
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
DispatcherServlet上下文:用于加载只对Spring Web MVC有效的Bean,如Controller、HandlerMapping、HandlerAdapter等;创建时以通用上下文为父上下文,创建完毕后将该上下文放在ServletContext:
childContext.setParent(rootContext);
servletContext.setAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT" + getServletName(), this.context);
4、spring mvc的典型配置
<!-- spring-context.xml中的配置 -->
<context:component-scan base-package="com.wind.pac" use-default-filters="true">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan> <!-- spring-mvc.xml中的配置 -->
<context:component-scan base-package="com.wind.pac" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
<context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.RestController" />
</context:component-scan>
5、DispatcherServlet默认配置
DispatcherServlet的默认配置在DispatcherServlet.properties(和DispatcherServlet类在一个包下)中,是当Spring配置文件中没有指定配置时使用的默认策略
注:eclipse下,web项目的运行目录为:workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\
参考:
第二章 Spring MVC入门 —— 跟开涛学SpringMVC
第三章 DispatcherServlet详解 ——跟开涛学SpringMVC
Spring MVC 使用介绍(二)—— DispatcherServlet的更多相关文章
- Spring MVC源码(二) ----- DispatcherServlet 请求处理流程 面试必问
前端控制器 前端控制器,即所谓的Front Controller,体现的是设计模式中的前端控制器模式.前端控制器处理所有从用户过来的请求.所有用户的请求都要通过前端控制器.SpringMVC框架和其他 ...
- Spring MVC 使用介绍(十五)数据验证 (二)依赖注入与方法级别验证
一.概述 JSR-349 (Bean Validation 1.1)对数据验证进一步进行的规范,主要内容如下: 1.依赖注入验证 2.方法级别验证 二.依赖注入验证 spring提供BeanValid ...
- Spring MVC 使用介绍(十四)文件上传下载
一.概述 文件上传时,http请求头Content-Type须为multipart/form-data,有两种实现方式: 1.基于FormData对象,该方式简单灵活 2.基于<form> ...
- Spring MVC 使用介绍(十三)数据验证 (一)基本介绍
一.消息处理功能 Spring提供MessageSource接口用于提供消息处理功能: public interface MessageSource { String getMessage(Strin ...
- 2017.3.31 spring mvc教程(二)核心流程及配置详解
学习的博客:http://elf8848.iteye.com/blog/875830/ 我项目中所用的版本:4.2.0.博客的时间比较早,11年的,学习的是Spring3 MVC.不知道版本上有没有变 ...
- Maven 工程下 Spring MVC 站点配置 (二) Mybatis数据操作
详细的Spring MVC框架搭配在这个连接中: Maven 工程下 Spring MVC 站点配置 (一) Maven 工程下 Spring MVC 站点配置 (二) Mybatis数据操作 这篇主 ...
- Spring MVC 简单介绍
Spring MVC 是典型的mvc架构,适合web开发. controler 输入输出的控制器,也是对外view提供数据的接口,调用service层. model 数据,由bean组成(相应表),关 ...
- Spring MVC的核心控制器DispatcherServlet的作用
关于Spring MVC的核心控制器DispatcherServlet的作用,以下说法错误的是( )? 它负责接收HTTP请求 加载配置文件 实现业务操作 初始化上下应用对象ApplicationC ...
- Spring MVC 使用介绍(十二)控制器返回结果统一处理
一.概述 在为前端提供http接口时,通常返回的数据需要统一的json格式,如包含错误码和错误信息等字段. 该功能的实现有四种可能的方式: AOP 利用环绕通知,对包含@RequestMapping注 ...
随机推荐
- jumpserver安装
一. 准备 Python3 和 Python 虚拟环境 1.1 安装依赖包 yum -y install wget sqlite-devel xz gcc automake zlib-devel o ...
- C#理解AutoResetEvent和ManualResetEvent
当在C#使用多线程时就免不了使用AutoResetEvent和ManualResetEvent类,可以理解这两个类可以通过设置信号来让线程停下来或让线程重新启动,其实与操作系统里的信号量很相似(汗,考 ...
- MySQL 5.6中如何定位DDL被阻塞的问题
在上一篇文章<MySQL 5.7中如何定位DDL被阻塞的问题>中,对于DDL被阻塞问题的定位,我们主要是基于MySQL 5.7新引入的performance_schema.metadata ...
- LNK2022: 元数据操作失败(8013118D): 重复类型(FactoryContext)中的布局信息不一致: (0x02000230)
1. c++项目 A 编译成A.lib文件 2. c++项目B引用这个A.lib文件 3. A项目存在一个类跟B项目一样,但是A项目其他文件需要这个类里面的某些东西,我将不需要的全部注释掉,然后编译A ...
- pycharm 报错:pycharm please specify a different SDK name
我在给项目配虚拟环境里的解释器的时候有没有遇到过这个问题的啊,就是一个正常的项目,解释器忽然丢了,解释器是配在虚拟环境里面的,再去选择解释器就一直报这个错,给现有项目添加虚拟环境的时候也是报这个错—— ...
- Python-待
内置函数总结 https://www.cnblogs.com/jason-lv/p/8243141.html https://www.cnblogs.com/pyyu/p/6702896.html 数 ...
- 多路选择器实现总线结构——Verilog
////////////////////////////////////////////////////////////////////////////////// //该程序完成通过多路选择器MUX ...
- MySQL 深入浅出数据库索引原理(转)
本文转自:https://www.cnblogs.com/aspwebchh/p/6652855.html 前段时间,公司一个新上线的网站出现页面响应速度缓慢的问题, 一位负责这个项目的但并不是搞技术 ...
- Cookie-parser
let express = require('express'); let app =new express(); // 引入cookie-parser; let cookieParser = req ...
- lumen 5.6 设置APP_KEY为32位长的随机字符串
在 App\Console\Commands下 添加以下内容的KeyGenerateCommand.php文件 <?php namespace App\Console\Commands; use ...