`
wangmengbk
  • 浏览: 288557 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

SpingMVC请求流程以及搭建环境

 
阅读更多

Spring mvc 概述

1.Spring MVC框架围绕DispatcherServlet这个核心展开,DispatcherServlet是 SpringMVC的总导演以及总策划,他负责截获请求并将其分派给相应的处理器进行处理。Spring MVC 框架包括注解驱动注释器、请求及响应的信息处理、试图解释、本地化解析、上传文件解析、异常处理以及变得标签绑定等内容。

2.Spring MVC 是基于Model2实现的技术框架。Model2是经典的MVC(Model,view,control)模型在web应用中的变体,这个改变主要源于HTTP 协议的无状态性。Model2的目的和MVC 一样。也是利用处理器分离模型、视图和控制,达到不同技术层级间松散层耦合的效果。提高系统灵活性、复用性、可维护性。

3.由于Spring MVC 是基于Model2 实现的框架。所以它底层的机制也是MVC,通过下图描述Spring MVC的整体架构。



 

 

从上图可以看出springMVC 从接受请求到返回响应,Spring MVC 框架的众多组件通力配合、各司其职,有条不紊的完成分内的工作。在整个框架中,DispatcherServlet 处于核心的位置,它负责协调和组织不同组件以完成请求处理并返回响应的工作。和大多数web MVC 框架一样,Spring MVC 通过一个前端Servlet接收所有的请求,并将具体工作委托给其他组件进行处理,DispatcherServlet 就是Spring MVC 的前端Servlet。下面我们对 SpringMVC处理请求的整体过程做一下高空俯瞰。

  • 1首先客户端发送HTTP请求,web应用服务器接收到这个请求,如果匹配DispatcherServlet的请求映射路径(在web.xml中指定),web 容器将该请求转交给DispatcherServlet处理。
  • 2 DispatcherServlet接收到这个请求后,将根据请求的信息(包括URL,HTTP方法、请求报文头,请求参数、Cookie等)以HandlerMapping 的配置找到处理请求的处理器(Handler)。可将HandlerMapping 看成路由器控制器,将Handler看成目标主机。
  • 3 当 DispatcherServlet 根据HandlerMapping 得到对应当前请求的Handler后,通过HandlerAdapter对Handler进行封装,再以统一的适配器接口调用 Handler。HandlerAdapter 是Spring MVC 的框架接口,顾名思义,HandlerAdapter 是一个适配器,它用统一的接口对各种handler方法进行调用。
  • 4 处理器完成业务逻辑的处理后将返回一个ModelAndView给DispatcherServlet,ModelAndView 包含了视图逻辑名和模型数据信息。
  • 5 ModelAndView 中包含的是“逻辑视图名”而非真正的视图对象,DispatcherServlet借由 ViewResolver完成逻辑视图名到真实视图对象的解析工作。
  • 6 当得到真实的视图对象 View 后, DispatcherServlet就使用这个View 对象对ModelAndView中的模型数据进行视图渲染。
  • 7 最终客户端得当的响应信息,可能是一个普通的HTML页面,也可能是一个XML或JSON 串,甚至是一张图片或一个PDF文档等不同的媒体形式。

4.配置 DispatcherServlet 对象并进行初始化,可以在web.xml 进行如下配置信息进行初始化:

        也可以参考一下配置进行SpringMVC + ibatis 环境创建。

<?xml version="1.0" encoding="UTF-8"?>

<web-app id="web" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<display-name>application</display-name>

 

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath*:com/wangmeng/**/server/META-INF/spring.xml,/WEB-INF/spring.xml</param-value>

</context-param>

<!-- 过滤器配置-->

 <filter>

<filter-name>encoding</filter-name>

<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

<init-param>

<param-name>encoding</param-name>

<param-value>UTF-8</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>encoding</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

 

<filter-mapping>

<filter-name>framework</filter-name>

<url-pattern>*.do</url-pattern>

</filter-mapping>

 

<servlet>

<servlet-name>springmvc</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<init-param>

                <description>Spring MVC 配置文件</description>

<param-name>contextConfigLocation</param-name>

<param-value>classpath*:com/wangmeng/**/server/META-INF/*-servlet.xml</param-value> 

  </init-param>

</servlet>

 

<servlet-mapping>

<servlet-name>springmvc</servlet-name>

<url-pattern>*.do</url-pattern>

</servlet-mapping>

 

<!-- Welcome file lists -->

<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

</welcome-file-list>

</web-app>

 

5.SpringMVC Servlet 配置文件(XXX-servlet.xml)

 

以下是XXX-servlet.xml 配置信息,对应web.xml 中的<init-param></init-param>配置信息。

<?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:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:mvc="http://www.springframework.org/schema/mvc"

xsi:schemaLocation="

http://www.springframework.org/schema/beans 

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/context 

http://www.springframework.org/schema/context/spring-context-3.0.xsd

http://www.springframework.org/schema/jee 

http://www.springframework.org/schema/jee/spring-jee-3.0.xsd

http://www.springframework.org/schema/tx 

http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

http://www.springframework.org/schema/aop 

http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<!-- 对web包中的所有类进行扫描,已完成Bean创建和自动依赖注入的功能 -->  

    <context:component-scan base-package="com.wangmeng.dpap"/>

 <!-- 相当于注册了DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter两个bean,

       配置一些messageconverter。即解决了@Controller注解的使用前提配置 

     -->  

    <mvc:annotation-driven />

<!-- 这里拦截器还有一种配置方法【针对路径进行配置】 推荐使用这个,方便直观-->  

   <!--  <mvc:interceptors>  

        <mvc:interceptor>   

            <mvc:mapping path="/springmvctest/*"/>  

            <bean  class="com.wangmeng.dpap.framework.springmvc.interceptor.ModuleInterceptor"></bean>  

        </mvc:interceptor>  

    </mvc:interceptors>   -->

<!-- 全局配置    --> 

    <mvc:interceptors>  

        <bean class="com.wangmeng.dpap.framework.springmvc.interceptor.ModuleInterceptor"></bean>  

        <!-- <bean class="com.deppon.dpap.framework.springmvc.interceptor.JSONInterceptor"></bean> --> 

    </mvc:interceptors> 

<!-- jsp页面解析器,当Controller返回XXX字符串时,先通过拦截器,然后该类就会在/WEB-INF/pages/目录下,查找XXX.jsp文件-->  

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  

        <property name="prefix" value="/WEB-INF/pages/"></property>  

        <property name="suffix" value=".jsp"></property>  

    </bean>  

</beans>

 

6.Spring.xml 配置文件 即在 web.xml 中配置<context-param></context-param> 初始化

<?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:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" 

xmlns:aop="http://www.springframework.org/schema/aop"

xsi:schemaLocation="

 http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd

http://www.springframework.org/schema/jee

http://www.springframework.org/schema/jee/spring-jee-3.0.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

<bean id="dataSource"

class="org.springframework.jdbc.datasource.DriverManagerDataSource">

<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />

<property name="url" value="jdbc:oracle:thin:@192.168.67.190:1521:dpap" />

<property name="username" value="bpapuser" />

<property name="password" value="bpapuser" />

</bean>

<!-- 数据连接管理 -->

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

<property name="dataSource" ref="dataSource" />

</bean>

<!-- 事务注解支持-->

<tx:annotation-driven transaction-manager="transactionManager" />

 

<!-- myBatis文件 -->

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

<property name="configLocation" value="/WEB-INF/ibatis.xml" />

<property name="mapperLocations"

value="classpath*:com/deppon/**/META-INF/ibatis/springmvc*.xml" />

<property name="dataSource" ref="dataSource" />

</bean>

<context:component-scan base-package="com.wangmeng" />

</beans>

 

 

  • 大小: 33.7 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics