SpringMVC<一> 基本结构与配置
刚刚踏入SpringMVC的学习,有一定Strust2的使用经验,边看书看博客,边总结,如有不对的地方还希望各位大佬多多指正。
Spring 响应过程与结构
(1)用户在客户端发送一个HTTP请求,Web服务器接受到该请求,如果在web.xml中匹配DispatcherServlet的请求映射路径,Web容器将该请求转交给DispatcherServlet处理。
(2)DispatcherServlet接受用户请求后,将根据请求信息以及HandlerMapping的配置找到处理请求的处理器(Controller)。可将HandlerMapping看成路由控制器,Controller看成目标主机。
(3)当DispatcherServlet根据HandlerMapping得到对应当前请求的Controller后,通过HandlerAdaptor对Controller进行封装,再以统一的适配器接口调用Controller。
HandlerAdapter是SpringMVC的框架级接口(适配器),使用统一的接口对各种Controller方法进行调用。
(4)处理器完成业务逻辑的处理后,将返回一个ModelAndView(也支持更多其他的返回类型,String、Map等,若视图逻辑名缺失,默认是转发到HTTP发起的页面 此处更多资讯可以查看SpringMVC Controller 返回值的可选类型)给DispatcherServlet,ModelAndView包含视图逻辑名和模块数据信息。
(5)DispatcherServlet借助ViewResolver完成逻辑视图名到真实视图对象的解析工作。
(6)当得到真实视图对象View后,DispatcherServlet就使用该View对象对ModelAndView中的数据模型进行视图渲染
(7)最终用户在客户端得到的响应信息,可能是一个普通的HTML页面,也可能是一个XML或者JSON串,甚至是一张图片或一个PDF文档等不同的媒体格式。
简单的说:DispatcherServlet相当与一个拦截收发站,拦截所有符合配置规则的请求,再转发到响应的Controller进行业务处理,业务处理后的数据交给ViewResovler进行视图渲染,完毕后,再
交会给DisptacherServlet进行与前端输出。
当然,我们更多的可能不会用到渲染的部分,作为后端我们更多的是获取数据,并进行业务处理,然后返回一个JSON给当前页面的AJAX进行后续的渲染。

请求处理流程
XML配置
WEB.XML
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Dispatcher-Servlet.xml
<?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"
xmlns:beans="http://www.springframework.org/schema/beans"
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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--开启注解-->
<context:component-scan base-package="Controller" 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.ControllerAdvice"/>
</context:component-scan> <!--Spring3.1开始的注解 HandlerMapping -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<!--Spring3.1开始的注解 HandlerAdapter -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
<!--图像解析器-->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
applicationContext.xml
<?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.xsd"> </beans>
目录结构:

部分图片来源:http://jinnianshilongnian.iteye.com/blog/1594806
SpringMVC<一> 基本结构与配置的更多相关文章
- springmvc+mybatis的两种配置和应用方式
一.不用写dao层实现的方式 1.导入依赖包,我的pom.xml文件配置如下: <project xmlns="http://maven.apache.org/POM/4.0.0&qu ...
- 使用Visual Studio Code开发Asp.Net Core WebApi学习笔记(七)-- 结构化配置
本篇将记录.Net Core里颇有特色的结构化配置的使用方法. 相比较之前通过Web.Config或者App.Config配置文件里使用xml节点定义配置内容的方式,.Net Core在配置系统上发生 ...
- SpringMVC介绍之约定优于配置
SpringMVC介绍之约定优于配置 所谓的约定优于配置就是指在程序开发过程中我们约定好一些规则可以使我们更少的进行配置和代码编写.就这么简单的一句话可能你还不是很懂什么是约定优于配置,没关系,看完后 ...
- springMVC注解方式+easyUI+MYSQL配置实例
刚接触springMVC,使用的注解方式,也在学习阶段,所以把自己学习到的记下来.本文利用springMVC从数据库读取用户信息为例,分享一下. 1.准备相关架包及资源.因为使用springMVC+e ...
- node-webkit学习(2)基本结构和配置
node-webkit学习(2)基本结构和配置 文/玄魂 目录 node webkit学习(2)基本结构和配置 前言 2.1 基本程序结构 2.2 package.json 2.2.1 必须的配置 ...
- SpringMVC之五:自定义DispatcherServlet配置及配置额外的 servlets 和 filters
相关文章 <Servlet3.0之四:动态注册和Servlet容器初始化> <SpringBoot中通过SpringBootServletInitializer如何实现组件加载> ...
- SpringMVC快速使用——基于XML配置和Servlet3.0
SpringMVC快速使用--基于XML配置和Servlet3.0 1.官方文档 https://docs.spring.io/spring-framework/docs/5.2.8.RELEASE/ ...
- 使用SpringMVC+mybatis+事务控制+JSON 配置最简单WEB
最近在总结一些项目的基础知识,根据公司最近的一些意向和技术路线,初步整理了一个简单的配置例子 1.使用springmvc代替strutsMVC 2.使用请求json数据串的方式代替传统 ...
- springmvc+mongodb+maven 项目搭建配置
操作步骤我就不再细化了 项目能运行,测试过了,先上配置,另一篇文章上代码,点击下载源码 项目结构 pom.xml <project xmlns="http://maven.apache ...
随机推荐
- Gtk-Message: Failed to load module “canberra-gtk-module”
编写wxPython程序时,总是报以下错误: Gtk-Message: Failed to load module “canberra-gtk-module” 解决办法:apt-get install ...
- 插入数据返回自增id及插入更新二合一
原文https://blog.csdn.net/dumzp13/article/details/50984413 JDBC: con.setAutoCommit(false); String sql ...
- bootloader svc 模式
bootloader 和操作系统都是工作在svc模式下 /* * set the cpu to SVC32 mode */ mrs r0,cpsr bic r0,r0,#0x1f orr r0,r0, ...
- PHP实现qq三方登录
除了qq第三方登录外.还有微博,微信等第三方登录 qq第三方登录,遵循oauth2.0协议 这里是说明http://www.cnblogs.com/yx520zhao/p/6616686.html q ...
- 学习python第十三天,函数5 装饰器decorator
定义:装饰器本质是函数,(装饰其他函数)就是为其他函数添加附加功能原则:1.不能修改被装饰的函数的源代码 2.不能修改装饰的函数的调用方式 实现装饰器知识储备1函数即变量2.高阶函数,满足2个条件之一 ...
- RepeatMasker使用
RM是library-based,通过相似性比对来识别重复序列,可以屏蔽序列中转座子重复序列和低复杂度序列(默认将其替换成N).使用数据库Dfam和Repbase. The Dfam database ...
- hdoj 1237 模拟
计算器 Problem Description 读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值. Input 测试输入包含若干测试用例,每个测试用例占一行,每行不超过 ...
- C++ 整型长度的获取 不同的系统
不同的系统中,C++整型变量中的长度位数是不同的,通常,在老式的IBM PC中,int 的位数为16位(与short相同),而在WINDOWS XP,Win7,vax等很多其他的微型计算机中,为32位 ...
- python——字符串的操作判断
s为字符串 s.isalnum() 所有字符都是数字或者字母,为真返回 Ture,否则返回 False. s.isalpha() 所有字符都是字母,为真返回 Ture,否则返回 False. s ...
- Permute Digits 915C
You are given two positive integer numbers a and b. Permute (change order) of the digits of a to con ...