Linux ·

Spring MVC配置详解

一、Spring MVC处理流程

  1.Spring MVC将所有请求都交由DispatchServlet进行处理。

  2.DispatchServlet获取HandlerMapping(处理映射器),然后找到对应的HandlerBean处理Controller请求,并返回一个ModelAndView对象。

  3.DispatchServlet查询一个或多个ViewResolver视图解析器对象, 并把视图渲染返回给前端。

二、相关配置

  首先配置web.xml,我们根据组件启动顺序:全局参数<context-param>、Listener监听器、Filter过滤器、Servlet、的顺序配置。

<web-app version="2.5" 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_2_5.xsd">

        <!-- 配置ContextLoaderListener监听器,使容器初始化spring配置文件 -->
        <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>

        <!--
            为每个请求过程绑定一个HibernateSession,它将由spring自动管理,无需手动开启和关闭
            此session的currentSession由SpringSessionContext管理而不是ThreadLocalSessionContext.
        -->
        <filter>
            <filter-name>openSessionInterceptor</filter-name>
            <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
        </filter>
        <filter-mapping>
            <filter-name>openSessionInterceptor</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>

        <!-- 字符集过滤器 -->
        <filter>
            <filter-name>encodingFilter</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>encodingFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>

        <!-- spring mvc 配置-->
        <servlet>
            <servlet-name>context</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath*:/spring-mvc.xml</param-value>
            </init-param>
        </servlet>

        <servlet-mapping>
            <servlet-name>context</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
</web-app>

   我们首先配置了org.springframework.web.context.ContextLoaderListener的Context监听器,我们以Tomcat为例:

  1、他启动的时候会去读取配置文件web.xml,首先读两个节点: <listener></listener> 和 <context-param></context-param>,然后创建一个ServletContext,整个web项目将共享这个Context。(意味着,我们这个项目将以spring MVC的context方式启动)

  2.容器将<context-param></context-param>的内容以键值对的形式交给Listener,配置中,我们配置了contextConfigLocation,也就是springmvc配置文件的位置,作为启动springContext的配置文件,这个值在容器启动的时候监听器执行contextInitialized方法可以拿到,然后 sc.getInitParameter(CONFIG_LOCATION_PARAM)获得配置文件路径,再根据配置文件路径初始化容器。(Spring源码如下)

1 String configLocationParam = sc.getInitParameter(CONFIG_LOCATION_PARAM);
2         if (configLocationParam != null) {
3             wac.setConfigLocation(configLocationParam);
4         }

  其中,ConfigLocationPraram是常量

1 public static final String CONFIG_LOCATION_PARAM = "contextConfigLocation";

  3.接下来是两个过滤器,分别是openSessionInterceptor为了给每个请求绑定HibernateSession的,在GetCurrentSession的时候可以由Spring进行统一管理,无需手动干扰,另外一个是字符集过滤器,将请求的编码统一。

  4.最后是DispatchServlet调度器,他是SpringMVC的核心,他拦截了所有请求,并将请求统一管理。注意:拦截路径必须写成<url-pattern>/</url-pattern>,不能写成<url-pattern>/*</url-pattern>,因为“/*”意为拦截所有请求,只要是请求一律拦截,而“/”意为将DispatcherServlet作为default Servlet(默认是org.apache.catalina.servlets.DefaultServlet),所有其他路径映射未匹配情况下才会交由它处理。而由于隐式映射的关系,使得 .jsp 扩展名被映射到静态资源进而被执行。 

  例如配置成“/*”执行过程中遇到的:

Spring MVC配置详解 Linux 第1张

  配置成“/*”他将拦截所有请求,当返回视图路径资源时,请求资源的请求被当成了servlet给拦截了,进而想要执行对应的Controller,发现没有对应的视图,但是根本不存在导致404错误。 

  接下来配置springMVC配置文件

<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/tool"
      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.3.xsd http://www.springframework.org/schema/tool http://www.springframework.org/schema/tool/spring-tool.xsd">

    <!-- 扫描spring注解包 -->
    <context:component-scan base-package="controll" 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.RequestMapping"/>
    </context:component-scan>

    <!-- 配置Spring默认返回路径前缀和后缀 -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>

   这是spring mvc配置文件,因为我们使用注解方式开发,所以要使用<context:component-scan/>扫描包,扫描注解,其实默认就是扫描注解的,所以可以直接写成:

<context:component-scan 

参与评论