zoukankan      html  css  js  c++  java
  • web.xml

    web.xml部署描述符文件是web应用的配置文件,位于WEB-INF目录下。Servlet容器根据web.xml中的配置来指定特定的Servlet处理客户端的具体请求。

    <util>标签

    使用<util>标签,必须在XML根元素中加入util名称空间(namespace):

    <web-app xmlns:util="http://http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd">

    分别使用<util:list>、<util:map>、<util:set>、<util:properties>等标签来取代ListFactoryBean、MapFactoryBean、SetFactoryBean、PropertiesFactoryBean对象。

    其中的<util:properties>标签还可以通过location属性指定其properties属性文件的位置。

    <util:properties id=”jdbcProps” location=”classpath:db.properties”/>

    在xml中使用spring表达式获取utlil元素中的值:

    <bean id=’datasource’ class=’xxx’>

        <property name=’url’ value=’#{jdbcProps.url}’></property>

    </bean>

    在setter方法前使用@Value注解获取util中的值

    @Value(#{jdbcProps.url})

    Private String url; 

                                     

    <context:property-placeholder location="classpath*:conf/conf*.properties"/>:PropertyPlaceholderConfigurer实现了BeanFactoryPostProcessor接口,它能够对<bean/>中的属性值进行外在化管理,提供单独的属性文件来管理相关属性。将properties加载到spring上下文中,接下来在定义bean的时候就能用${xx.yy}来访问相关属性。

    classpath*:是为了同时加载多个匹配的文件,而classpath:只能加载匹配的第一个文件。

    在Spring中classpath一般指src目录。

     

    <context-param>元素

    用来声明整个web应用范围内的上下文初始化参数,该元素只能出现在与文档有关的元素(icon、display-name或description)之后及filter、filter-mapping、listener或 servlet元素之前。

    Tomcat容器初始化过程:

    1. 在启动Web项目时,容器(Tomcat)会读web.xml配置文件中的<listener>和<contex-param>两个节点。

    2. 容器创建一个ServletContext(上下文),整个web应用都使用这个上下文。

    3. 容器将读取到<context-param>元素的值传给ServletContext。

    4. 容器创建<listener></listener>中的实例。

    5. 在监听的类中会有一个contextInitialized(ServletContextEvent event)初始化方法,在这个方法中可以通过event.getServletContext().getInitParameter("contextConfigLocation") 来得到context-param 设定的值。在这个类中还必须有一个contextDestroyed(ServletContextEvent event) 销毁方法,用于关闭应用前释放资源,比如数据库连接的关闭。

    6. 得到这个context-param的值之后,就可以做一些操作了。此时Web项目还没有完全启动。

    7. 由上可知容器对于web.xml的加载过程是context-param -> listener -> fileter -> servlet

    <context-param><init-param>区别
    (1) <context-param>整个application范围内的参数,存放在ServletContext中:

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/applicationContext-*.xml</param-value>
    </context-param>

    servlet中获取的方式:

    String paramValue = getServletContext().getInitParameter("contextConfigLocation")

     (2) <init-param>是servlet范围内的参数,只能在对应的servlet或jsp页面中获取:

    <servlet>
        <servlet-name>MainServlet</servlet-name>
        <servlet-class>com.wes.controller.MainServlet</servlet-class>
        <init-param>
           <param-name>param</param-name>
           <param-value>avalible in servlet init()</param-value>
        </init-param>
    </servlet>

    Servlet中获取的方式:

    String paramValue = this.getInitParameter("param");

     

    <listener>元素

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

    作用:

    Servlet规范中定义的监听器,用来监听Servlet容器产生的事件并进行相应的处理。

    而上述的ContextLoaderListener是Spring提供的ServletContextListener的一个实现类,它会在tomcat容器初始化时查找WEB-INF下的applicationContext.xml文件(没有contextConfigLocation时),创建Spring容器。Spring容器根据其配置文件管理bean对象(bean默认为singleon作用域,applicationContext启动时会将其实例化)。

     

    <load-on-startup>元素

    <!—tomcat容器初始化时创建springmvc的前端控制器 -->

    <servlet>
             <servlet-name>taotao-manager</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>
             <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
             <servlet-name>taotao-manager</servlet-name>
             <url-pattern>/</url-pattern>
    </servlet-mapping>

    用于标记tomcat容器是否在启动的时候就加载这个servlet(实例化并调用其init()方法)。它的值必须是一个整数,表示servlet应该被载入的顺序。正数的值越小,该servlet的优先级越高,应用启动时就越先加载。当值相同时,容器就会自己选择顺序来加载。当值小于0或者没有指定时,则表示容器在该servlet被请求时才会去加载。当值为0或者大于0时,表示容器在应用启动时就加载并初始化该servlet。

  • 相关阅读:
    智器SmartQ T7实体店试用体验
    BI笔记之SSAS库Process的几种方案
    PowerTip of the Day from powershell.com上周汇总(八)
    PowerTip of the Day2010071420100716 summary
    PowerTip of the Day from powershell.com上周汇总(十)
    PowerTip of the Day from powershell.com上周汇总(六)
    重新整理Cellset转Datatable
    自动加密web.config配置节批处理
    与DotNet数据对象结合的自定义数据对象设计 (二) 数据集合与DataTable
    在VS2003中以ClassLibrary工程的方式管理Web工程.
  • 原文地址:https://www.cnblogs.com/kevin2chen/p/6718872.html
Copyright © 2011-2022 走看看