项目小结SpringMVC+hibernate框架 web.xml 分析(2)
项目总结SpringMVC+hibernate框架 原理(MVC)(1)
接上章继续:
所需要的jar
在spring 的官方API 文档中,给出所有包的作用概述,现列举常用的包及相关作用:
org.springframework.aop-3.0.5.RELEASE.jar :与Aop 编程相关的包
org.springframework.beans-3.0.5.RELEASE.jar :提供了简捷操作bean 的接口
org.springframework.context-3.0.5.RELEASE.jar :构建在beans 包基础上,用来处理资源文件及国际化。
org.springframework.core-3.0.5.RELEASE.jar :spring 核心包
org.springframework.web-3.0.5.RELEASE.jar :web 核心包,提供了web 层接口
org.springframework.web.servlet-3.0.5.RELEASE.jar :web 层的一个具体实现首先看看我的web的 (部分代码)
<?xml version="1.0"encoding="UTF-8"?> <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"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/applicationContext.xml</param-value> </context-param> <!-- 字符过滤器配置 用于指定WEB容器的过滤器,在请求和响应对象在Servlet处理之前和之后,可以通过此过滤器对这两个对象进行处理。--> <filter> <display-name>encodingfilter</display-name> <filter-name>setEncodingFilter</filter-name> <filter-class>common.filter.SetCharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>GBK</param-value> </init-param> <init-param> <param-name>ignore</param-name> <param-value>false</param-value> </init-param> </filter> <filter-mapping> <filter-name>setEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--OpenSessionInViewFilter 说明一下OpenSession in View的作用,就是允许在每次的整个request的过程中使用同一个hibernate session,可以在这个request任何时期lazy loading数据。 如果是singleSession=false的话,就不会在每次的整个request的过程中使用同一个hibernate session,而是每个数据访问都会产生各自的seesion,等于没有Open Session in View。 OpenSessionInViewFilter默认是不会对session 进行flush的,并且flush mode 是never --> <filter> <filter-name>OpenSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> <init-param> <param-name>flushMode</param-name> <param-value>AUTO</param-value> </init-param> </filter> <filter-mapping> <filter-name>OpenSessionInViewFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- SpringMVC在Web容器中的启动类,负责Spring IoC容器在Web上下文中的初始化。 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 自定义的监听器 --> <listener> <listener-class>common.listener.XiangmuSessionListener</listener-class> </listener> <!-- DispatcherServlet主要用作职责调度工作 --> <servlet> <servlet-name>system</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/system-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet> <servlet-name>menu</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/menu-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- 为导出html类型的报表图片而加如的servlet --> <servlet> <servlet-name>ImageServlet</servlet-name> <servlet-class>net.sf.jasperreports.j2ee.servlets.ImageServlet</servlet-class> </servlet> <!-- 这是我的J2EE组件的描述--> <servlet> <description>Thisis the description of my J2EE component</description> <display-name>Thisis the display name of my J2EE component</display-name> <servlet-name>GetUUIDServlet</servlet-name> <servlet-class>common.util.GetUUIDServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>system</servlet-name> <url-pattern>/system/*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>menu</servlet-name> <url-pattern>/menu/*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>GetUUIDServlet</servlet-name> <url-pattern>/common/GetUUIDServlet</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>ImageServlet</servlet-name> <url-pattern>/servlets/image</url-pattern> </servlet-mapping> <!-- session存活时间--> <session-config> <session-timeout>60</session-timeout> </session-config> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> <login-config> <auth-method>BASIC</auth-method> </login-config> </web-app>
web.xml的配置中<context-param>配置作用
1. 启动一个WEB项目的时候,容器(如:Tomcat)会去读它的配置文件web.xml.读两个节点: <listener></listener> 和<context-param></context-param>
2.紧接着,容器创建一个ServletContext(上下文),这个WEB项目所有部分都将共享这个上下文.
3.容器将<context-param></context-param>转化为键值对,并交给ServletContext.
4.容器创建<listener></listener>中的类实例,即创建监听.
5.在监听中会有contextInitialized(ServletContextEventargs)初始化方法,在这个方法中获得ServletContext= ServletContextEvent.getServletContext();
context-param的值 =ServletContext.getInitParameter("context-param的键");
6.得到这个context-param的值之后,你就可以做一些操作了.注意,这个时候你的WEB项目还没有完全启动完成.这个动作会比所有的Servlet都要早.
换句话说,这个时候,你对<context-param>中的键值做的操作,将在你的WEB项目完全启动之前被执行.
7.举例.你可能想在项目启动之前就打开数据库.
那么这里就可以在<context-param>中设置数据库的连接方式,在监听类中初始化数据库的连接.
8.这个监听是自己写的一个类,除了初始化方法,它还有销毁方法.用于关闭应用前释放资源.比如说数据库连接的关闭.
web.xml里面可以定义两种参数:
(1)application范围内的参数,存放在servletcontext中,在web.xml中配置如下:
先看代码后看解释
<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/applicationContext.xml</param-value> </context-param>
contextConfigLocation 参数定义了要装入的 Spring 配置文件。原理说明如下:
利用ServletContextListener实现。
Spring 提供ServletContextListener 的一个实现类ContextLoaderListener,该类可以作为listener 使用它会在创建时自动查找WEB-INF/ 下的applicationContext.xrnl 文件。
因此,如果只有一个配置文件,并且文件名为applicationContext.xml ,则只需在web.xml文件中增加如下代码即可:
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
如果有多个配置文件需要载入,则考虑使用<context-para即元素来确定配置文件的
文件名。由于ContextLoaderListener加载时,会查找名为contextConfigLocation的参数。
因此,配置context-param时参数名字应该是contextConfigLocation。
带多个配置文件的web.xml 文件如下:
<1-- XML 文件的文件头二〉 <?xml version="l.O" encoding="工80-8859-1"?> < 1-- web.xm1 文件的DTD 等信息一〉 <!DOCTYPE web-app PUBLIC "-//Sun Microsystems. 工口c.//DTD Web Application 2.3//EN" ''http://java.sun.com/dtd/web-app_2_3.dtd''> <web-app> <!一确定多个配置文件> <context-param> <1-- 参数名为contextConfigLocation…〉 <param-name>contextConfigLocation</param-name> <!一多个配置文件之间以 "," 隔开二〉 <param-value>/WEB-工NF/daoContext.xml,/WEB-INF/applicationContext.xml</param-value> </context-param> <!-- 采用listener创建Applicat工onContext 实例--> <listener> <listener-class>org.spr工ngframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
如果没有contextConfigLocation 指定配置文件,则Spring 自动查找applicationContext. xrnl 配置文件。如果有contextConfigLocation,则利用该参数确定的配置文件。
该参数指定的一个字符串, Spring 的ContextLoaderListener负责将该字符串分解成多个配置文件,逗号","、空格" "及分号";"都可作为字符串的分割符。
如果既没有applicationContext. xrnl 文件,也没有使用contextConfigLocation参数确定配置文件,或者contextConfigLocation确定的配置文件不存在。都将导致Spring 无法
加载配置文件或无法正常创建ApplicationContext 实例配置一个spring为加载而设置的servlet可以达到同样效果.采用load-on-startup Servlet 实现。
Spring 提供了一个特殊的Servllet 类:ContextLoaderServlet。该Servlet 在启动时,会自动查找WEB-IN日下的applicationContext. xml 文件。
当然,为了让ContextLoaderServlet 随应用启动而启动,应将此Servlet 配置成load-on-startup 的Servleto load-on-startup 的值小一点比较合适,
因为要保证ApplicationContext 优先创建。如果只有一个配置文件,并且文件名为applicationContext.xml ,则在
web.xml 文件中增加如下代码即可:
<servlet> <servlet-name> context </servlet-name> <servlet-class> org.springframework.web.context.ContextLoaderServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet>
(2)servlet范围内的参数,只能在servlet的init()方法中取得,在web.xml中配置如下:
<servlet> <servlet-name>system</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/system-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>system</servlet-name> <url-pattern>/system/*</url-pattern> </servlet-mapping>