zoukankan      html  css  js  c++  java
  • ssm框架整合入门系列——编写ssm整合的关键配置文件(web.xml)

    编写ssm整合的关键配置文件(web.xml)


    前言

    web.xml,一个Tomcat工程中最重要的配置文件。web.xml没有其实也可以----只要你确定你的项目里面不需要任何过滤器、监听器、Servlet等等

    在启动一个WEB项目的时候,WEB容器(比如tomcat)会去读取它的配置文件web.xml,读取到不同的节点时,WEB容器就会创建相应的过滤器、监听器等为这个web项目服务。

    如果你之前学过servlet肯定知道web.xml的加载顺序为:context-param->listener->filter->servlet。


    完整web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     	xmlns="http://java.sun.com/xml/ns/javaee"
      	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
       	id="WebApp_ID" version="3.0">
      <display-name>ssm-crud</display-name>
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
      </welcome-file-list>
      
      <!-- 启动Spring容器 -->
      <!-- needed for ContextLoaderListener -->
      <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>
      
      <!-- SpringMVC的前端控制器,拦截所有请求 -->
      <servlet>
      	<servlet-name>springDispatcherServlet</servlet-name>
      	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      	<!--
    	  	<init-param>
    	  		<param-name>contextConfigLoction</param-name>
    	  		<param-value>location</param-value>
    	  	</init-param>
      	 -->
      	 <load-on-startup>1</load-on-startup>
      </servlet>
      <!-- Map all requests to the DispatcherServlet for.. -->
      <servlet-mapping>
      	<servlet-name>dispatcherServlet</servlet-name>
      	<url-pattern>/</url-pattern>
      </servlet-mapping>
      
      <!-- 字符编码过滤器,一定要放在所有过滤器之前 -->
      <filter>
      	<filter-name>CharacterEncodingFilter</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>
      	<init-param>
    		<param-name>forceRequestEncoding</param-name>
    		<param-value>true</param-value>
      	</init-param>
      	<init-param>
      		<param-name>forceResponseEncoding</param-name>
      		<param-value>true</param-value>
      	</init-param>
      </filter>
      <filter-mapping>
      	<filter-name>CharacterEncodingFilter</filter-name>
      	<url-pattern>/*</url-pattern>
      </filter-mapping>
      
      <!-- Rest风格的URI 将页面普通的post请求转为指定的delete或者put请求 -->
      <filter>
      	<filter-name>HiddenHttpMethodFilter</filter-name>
      	<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
      </filter>
      <filter-mapping>
      	<filter-name>HiddenHttpMethodFilter</filter-name>
      	<servlet-name>/*</servlet-name>
      </filter-mapping>
      
    </web-app>
    

    该项目的web.xml内容解析

    在ssm-crud项目下打开web.xml

    1.contextConfigLocation配置解析

    如下代码:

    <!-- 启动Spring容器 -->
      <!-- needed for ContextLoaderListener -->
      <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>
    

    作用:该元素用来声明应用范围(整个WEB项目)内的上下文初始化参数。
    param-name 设定上下文的参数名称。必须是唯一名称
    param-value 设定的参数名称的值
    contextConfigLocation 参数定义了要装入的 Spring 配置文件。
    与Spring相关的配置文件必须要以"applicationContext-"开头,要符合约定优于配置的思想

    看到有一篇相关的详解:https://blog.csdn.net/tiantiandjava/article/details/8964912

    和contextConfigLocation配合使用的是ContextLoaderListener
    在myeclipse中使用快捷键 ctrl+shift+t可以直接打开它的源码文件,如图:
    contextLoaderListener
    可以看到,它是一个继承于ContextLoader和实现了ServletContextListener接口的一个类。如有需要可以它们的源码,这里不关注。
    那么ContextLoaderListener的作用是什么?

    ContextLoaderListener的作用就是启动Web容器时,读取在contextConfigLocation中定义的xml文件,自动装配ApplicationContext的配置信息,并产生WebApplicationContext对象,然后将这个对象放置在ServletContext的属性里,这样我们只要得到Servlet就可以得到WebApplicationContext对象,并利用这个对象访问spring容器管理的bean。
    简单来说,就是上面这段配置为项目提供了spring支持,初始化了Ioc容器。

    作者:walidake 链接:https://www.jianshu.com/p/523bfddf0810 來源:简书
    简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

    当然添加了以上配置后,还有在 src/main/resourse目录下新建一个applicationContext.xml文件(new spring bean Definition)。
    applicationContext.xml配置信息会在下一篇文章。

    2.dispatcherServlet

    <!-- SpringMVC的前端控制器,拦截所有请求 -->
      <servlet>
      	<servlet-name>dispatcherServlet</servlet-name>
      	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      	<!-- 如果不写这里需要在web-inf文件夹里配置dispatcherServlet-servlet.xml文件
    	  	<init-param>
    	  		<param-name>contextConfigLoction</param-name>
    	  		<param-value>location</param-value>
    	  	</init-param>
      	 -->
      	 <load-on-startup>1</load-on-startup>
      </servlet>
      <!-- Map all requests to the DispatcherServlet for.. -->
      <servlet-mapping>
      	<servlet-name>dispatcherServlet</servlet-name>
      	<url-pattern>/</url-pattern>
      </servlet-mapping>
      
    

    配置DispatcherServlet表示,该工程将采用springmvc的方式。启动时也会默认在/WEB-INF目录下查找XXX-servlet.xml作为配置文件.

    dispatcherServlet是名字名字,该文件中将配置两项重要的mvc特性:HandlerMapping,负责为DispatcherServlet这个前端控制器的请求查找Controller;

    这里有一篇对于dispatcherServlet更详细的文章:https://juejin.im/post/58eb3c34b123db1ad06796c6

    3.CharacterEncodingFilter 字符编码过滤器

    <!-- 字符编码过滤器,一定要放在所有过滤器之前 -->
      <filter>
      	<filter-name>CharacterEncodingFilter</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>
      	<init-param>
    		<param-name>forceRequestEncoding</param-name>
    		<param-value>true</param-value>
      	</init-param>
      	<init-param>
      		<param-name>forceResponseEncoding</param-name>
      		<param-value>true</param-value>
      	</init-param>
      </filter>
      <filter-mapping>
      	<filter-name>CharacterEncodingFilter</filter-name>
      	<url-pattern>/*</url-pattern>
      </filter-mapping>
    

    过滤器基础知识

    <filter>指定一个过滤器。
    <filter-name>用于为过滤器指定一个名字,该元素的内容不能为空。
    <filter-class>元素用于指定过滤器的完整的限定类名。
    <init-param>元素用于为过滤器指定初始化参数,它的子元素<param-name>指定参数的名字,<param-value>指定参数的值。
    在过滤器中,可以使用FilterConfig接口对象来访问初始化参数。
    <filter-mapping>元素用于设置一个 Filter 所负责拦截的资源。一个Filter拦截的资源可通过两种方式来指定:Servlet 名称和资源访问的请求路径
    <filter-name>子元素用于设置filter的注册名称。该值必须是在<filter>元素中声明过的过滤器的名字
    <url-pattern>设置 filter 所拦截的请求路径(过滤器关联的URL样式)

    CharacterEncodingFilter类注释源码译文片段:

    Servlet过滤器,允许用户为请求指定字符编码。
    *这很有用,因为当前的浏览器通常不设置字符
    *编码,即使在HTML页面或表单中指定。
    *如果请求尚未应用,则此过滤器可以应用其编码
    *指定编码,或在任何情况下强制执行此过滤器的编码
    *(“forceEncoding”=“true”)。 在后一种情况下,编码也将是
    *作为默认响应编码应用(虽然这通常会被覆盖
    *通过视图中设置的完整内容类型)。

    一句话总结:使用过滤器设置项目的编码默认使用utf-8,至于其中的两个参数forceRequestEncoding,forceResponseEncoding,是该类的逻辑要求,可以参考源码:CharacterEncodingFilter

    一篇关于filter 过滤器的文章:Filter 过滤器

    4.HiddenHttpMethodFilter

    • URI (Uniform Resource Identifier)标识、定位任何资源的字符串
    • URL是URI的子集
    <!-- Rest风格的URI 将页面普通的post请求转为指定的delete或者put请求 -->
      <filter>
      	<filter-name>HiddenHttpMethodFilter</filter-name>
      	<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
      </filter>
      <filter-mapping>
      	<filter-name>HiddenHttpMethodFilter</filter-name>
      	<servlet-name>/*</servlet-name>
      </filter-mapping>
    

    HiddenHttpMethodFilter类注释源码译文片段:

    • {@link javax.servlet.Filter}将发布的方法参数转换为HTTP方法,可通过{@link HttpServletRequest#getMethod()}检索。由于浏览器目前只有 *支持GET和POST,这是一种常见技术 -
    • 例如Prototype库使用 - 是使用带有附加隐藏表单字段的普通POST( _method </ code>) 传递“真正的”HTTP方法。此过滤器读取该参数并进行更改 * {@link HttpServletRequestWrapper#getMethod()}相应地返回值。

    它的文章:HiddenHttpMethodFilter

  • 相关阅读:
    面试题汇总--1
    行内元素与块级元素
    前端面试题整理---JS基础
    springmvc处理一个请求的全流程
    反射+动态代理+类加载+JVM
    json注解及序列化
    加密
    Thread setUncaughtExceptionHandler
    RunTime.getRunTime().addShutdownHook 添加钩子
    MySQL日志
  • 原文地址:https://www.cnblogs.com/famine/p/9792253.html
Copyright © 2011-2022 走看看