• Spring MVC 的 研发之路 (二)


    二、web.xml的简单配置介绍1

    1、启动Web项目时,容器回去读web.xml配置文件里的两个节点<context-param>和<listener>
    2、接着容器会创建一个servletContext(上下文)应用范围内都能使用这个上下文
    3、接着容器会将读取到<context-param>转化为键值对,并交给ServletContext。
    4、通过<listener>创建监听
    5、在监听的类中会有一个contextInitialized(ServletContextEvent event)初始化方法,在这种方法中能够通过event.getServletContext().getInitParameter("contextConfigLocation") 来得到context-param 设定的值。在这个类中还必须有一个contextDestroyed(ServletContextEvent event) 销毁方法.用于关闭应用前释放资源,比方说数据库连接的关闭。
    6、得到这个context-param的值之后,你就能够做一些操作了.注意,这个时候你的WEB项目还没有全然启动完毕.这个动作会比全部的Servlet都要早。

    一、

    <span style="font-size:18px;"><context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                classpath:/spring/application-datasource.xml
            </param-value>
    </context-param></span></span>
    

    该元素用来声明应用范围内的上下文初始化參数(整个web项目中)比如对数据库连接池的配置等

    param-name 设定上下文的參数名称。名称必须唯一

    param-value 设定的參数名称的值  指定响应的xml文件名称,假设有多个文件能够写在一起用“,”分隔

    能够採用通配符形式applicationContext-*.xml  能够将此类开头的文件所有加载

    1、能够直接将之放到/web-inf下

    2、放到classpath下 classpath:/spring/application-datasource.xml

    如:

    <span style="font-size:18px;"><!-- Context Configuration locations for Spring XML files -->  
    <context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value>  
    </context-param> </span><span style="font-size: 14px;">
    </span></span>


    以此供web容器载入


     二、

    <span style="font-size:18px;"><listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener></span></span>

    listener标签是用于监听的,ContextLoaderListener实现了ServletContextListener接口,能够监听全部的servlet、session、request等接口。
    ContextLoaderListener的作用就是启动Web容器时。自己主动装配ApplicationContext的配置信息,它实现了ServletContextListener这个接口,在web.xml中配置这个监听器,启动容器时,就是默认运行它的实现方法。


    ServletContext:每一个web应用都有一个servletContext与之相关.servletContext对象在应用启动的时候被创建,在应用关闭的时候被销毁。

    ervletContextListener: 使用listener接口。开发人员可以在为client请求提供服务之前向ServletContext中加入随意的对象。

    这个对象在ServletContext启动的时候被初始化,然后在ServletContext整个执行期间都是可见的。

    该接口拥有两个方法例如以下所看到的:

    三、
    <filter>
        <filter-name></filter-name>
        <filter-class></filter-class>
    </filter>

    filter运行是依照web.xml配置的filter-mapping先后顺序进行运行
    <span style="font-size:18px;">    <filter>
            <filter-name>Character 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>Character Encoding</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping></span><span style="font-size: 14px;">
    </span>

    以上是防止乱码
    <span style="font-size:18px;"><filter>
            <filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
            <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
            <init-param>
                <param-name>entityManagerFactoryBeanName</param-name>
                <param-value>entityManagerFactory</param-value>
            </init-param>
        </filter>
    
        <filter-mapping>
            <filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping></span><span style="font-size: 14px;">
    </span>

    在Java Web项目中使用Hibernate常常会遇到LazyInitializationException。

    这是由于controller和model层(java代码)将通过JPA的一些启用了延迟载入功能的领域(如用getRefrence()方法或者在关联关系中採用fetch=FetchType.LAZY)返回给view层(jsp代码)的时候,由于载入领域对象的JPA Session已经关闭,导致这些延迟载入的数据訪问异常。

    这时就能够使用OpenEntityManagerInViewFilter来将一个JPAsession与一次完整的请求过程相应的线程相绑定。

    假设没使用OpenEntityManagerInViewFilter,session会在service.find()方法后就被关闭。用了以后session在整个view层结束后才关闭。

    四、

    <span style="font-size:18px;"><servlet>
            <servlet-name>mvc-dispatcher</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
    
    	<servlet-mapping>
    		<servlet-name>mvc-dispatcher</servlet-name>
    		<url-pattern>/</url-pattern>
    	</servlet-mapping></span><span style="font-size: 14px;">
    </span>

    DispatcherServlet是前端控制器设计模式的实现,提供Spring Web MVC的集中訪问点。并且负责职责的分派。并且与Spring IoC容器无缝集成,从而能够获得Spring的全部优点。

     详细请參考第二章的图2-1。

     

    DispatcherServlet主要用作职责调度工作。本身主要用于控制流程,主要职责例如以下:

    1、文件上传解析,假设请求类型是multipart将通过MultipartResolver进行文件上传解析;

    2、通过HandlerMapping,将请求映射到处理器(返回一个HandlerExecutionChain,它包含一个处理器、多个HandlerInterceptor拦截器)。

    3、  通过HandlerAdapter支持多种类型的处理器(HandlerExecutionChain中的处理器);

    4、通过ViewResolver解析逻辑视图名到详细视图实现;

    5、本地化解析;

    6、渲染详细的视图等;

    7、假设运行过程中遇到异常将交给HandlerExceptionResolver来解析。

    load-on-startup:表示启动容器时初始化该Servlet;

    url-pattern:表示哪些请求交给Spring WebMVC处理, “/” 是用来定义默认servlet映射的。也能够如“*.html”表示拦截全部以html为扩展名的请求。

    DispatcherServlet默认使用WebApplicationContext作为上下文。Spring默认配置文件为“/WEB-INF/[servlet名字]-servlet.xml”。


    以上是看了众多关于web.xml文章中整理出来的easy理解的部分。
  • 相关阅读:
    平衡二叉树 JAVA实现 亲测可用
    八大排序算法 JAVA实现 亲自测试 可用!
    Java 函数传入参数后,究竟发生了什么?java函数传参数原理解析
    运行错误:应用程序无法启动因为并行配置不正确。the application has failed to start because its side-by-side configuration is incorrect 解决方法
    mysql在linux下修改存储路径
    redis订阅与发布系统
    PHP Math常量
    PHP Math函数
    php 字符串函数
    PHP数组函数
  • 原文地址:https://www.cnblogs.com/gccbuaa/p/7058709.html
Copyright © 2020-2023  润新知