springMVC---配置文件解析(web.xml)

前言


接着前面一片文章SpringMVC---IDEA 搭建SpringMVC工程,继续解析各配置文件是用来干嘛的。只有弄懂每一个配置项的意义,才能更好的掌握springMVC.

问题


web.xml文件详解:

1、web.xml文件作用是什么?
  web.xml文件的作用是配置web工程启动,对于一个web工程来说,web.xml可以有也可以没有,如果存在web.xml文件;web工程在启动的时候,web容器(tomcat容器)会去加载web.xml文件,然后按照一定规则配置web.xml文件中的组件。

2、web容器加载web.xml文件的规则是怎样的?
  web容器加载顺序:ServletContext -> context-param -> listener -> filter ->servlet ;不会因在web.xml中的书写顺序改变:   
  a、web容器启动后,会去加载web.xml文件,读取listener和context-param两个节点
  b、创建一个ServletContext(Servlet上下文)这个上下文供所有部分共享
  c、容器将context-param转换成键值对,交给ServletContext
  d、接着按照上述顺序继续执行


SpringMVC的web.xml文件


1、配置代码及流程图示例:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <!--Spring MVC 配置 并添加监听-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:/spring/*</param-value>
  </context-param>

  <!-- 字符过滤器 传值乱码-->
  <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>

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <!--配置前端控制器 进行请求分发 DispatcherServlet本质也是一个Servlet -->
  <servlet>
    <!--名字可以自定义-->
    <servlet-name>SpringMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring/springmvc.xml</param-value>
    </init-param>
    <!--标记容器启动的时候就启动这个servlet-->
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>SpringMVC</servlet-name>
    <!--拦截所有-->
    <url-pattern>/</url-pattern>
  </servlet-mapping>

</web-app>
复制代码

2、加载顺序
a、首先加载Spring容器加载器:ContextLoaderListener

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
复制代码

  查看ContextLoaderListener源码可以发现其实现了ServletContextListener并继承了ContextLoader类,ServletContextListener主要用于监听web容器的启动和销毁,ContextLoader用于web容器启动后加载springApplicationContext上下文。
  ServletContextListener的两个方法为contextInitialized,contextDestroyed,主要用来监听Web应用的生命周期,当web应用初始化或者结束时会触发ServletContextEvent,并由监听器监听触发其他操作。
  ContextLoader主要用于加载上下文:当web服务器开启时候,触发ServletContextEvent并被ContextLoaderListener监听到,此时执行,ContextLoaderListener中的contextInitialized方法,此方法为ContextLoader中的方法,查看源码可以发现其创建了WebApplicationContext,并将springApplicationContext中的bean注册到容器中供项目使用。

b、加载过滤器Filter(此处以编码过滤器CharacterEncodingFilter为例)此过滤器的作用就是设置请求参数的编码为UTF-8.

<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>
复制代码

c、加载Servlet:初始化DispatcherServlet,在SpringMVC架构中,DispatchServlet负责请求分发,起到控制器的作用

<!--配置前端控制器 进行请求分发 DispatcherServlet本质也是一个Servlet -->
  <servlet>
    <!--名字可以自定义-->
    <servlet-name>SpringMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring/springmvc.xml</param-value>
    </init-param>
    <!--标记容器启动的时候就启动这个servlet-->
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>SpringMVC</servlet-name>
    <!--拦截所有-->
    <url-pattern>/</url-pattern>
  </servlet-mapping>
复制代码
以下是一个简单的springmvc-config.xml配置文件的例子: ```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" 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="com.example.controller"/> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean> <!-- 配置静态资源处理 --> <mvc:resources mapping="/static/**" location="/static/"/> <!-- 配置RequestMappingHandlerAdapter --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/plain;charset=UTF-8</value> <value>text/html;charset=UTF-8</value> </list> </property> </bean> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>application/json;charset=UTF-8</value> </list> </property> </bean> </list> </property> </bean> <!-- 配置RequestMappingHandlerMapping --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> </beans> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值