【1.在web.xml里配置Listener】
xml 代码如下:
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
如果在web.xml里给该Listener指定要加载的xml,如:
xml代码如下:
<!-- spring config -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>
则会去加载相应的xml,而不会去加载/WEB-INF/下的applicationContext.xml。
但是,如果没有指定的话,默认会去/WEB-INF/下加载applicationContext.xml。
以前经常报org.springframework.web.context.ContextLoaderListener这个错误
是因为src下的applicationContext*.xml文件没有加载到tomcat,而且所有的java文件
也没有重新编译。所以才出现这个错误。
【2.src 与 WEB-INF 的区别。】
根据上面的例子:在<context-param>contextConfigLocation</context-param>标签中,如果不指定
<param-value>的值,则默认是加载WEB-INF/下的applicationContext.xml文件。
上面指定为src目录下的所有以applicationContext开头的xml文件
下面来看一下log4jConfigLocation
一般我们直接将log4j.properties放置在src目录下,这样系统自动会找到的,
其实就是放在WEB-INF/classes文件下。这个路径在classpath下,所以直接就能找到。
在web.xml中配置servlet,并将log4jConfigLocation加入到Servlet中,让其Server启动即运行:
<servlet>
<servlet-name>your servlet</servlet-name>
<servlet-class>your servelt class</servlet-class>
<init-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
这时log4j.properties文件放在WEB-INF目录下。
【3.下面我们来深入了解一下<context-param> 与 <init-param>】
其实一目了然<init-param>是tomcat服务器,创建servlet的时候才加载。
而<context-param>是tomcat启动之后就加载。
context-param 是ServletContext 创建的,属于整个tomcat服务器中。
init-param 是有servlet创建的。
init-param属于一个servlet所有,
context-param属于整个应用程序所有 ,不仅是在servlet中可以得到,jsp文件中也可以得到.
在jsp中config就相当于这里的servletContext.
web.xml里面可以定义两种参数:
(1)application范围内的参数,存放在servletcontext中,在web.xml中配置如下:
xml 代码
<context-param>
<param-name>context/param</param-name>
<param-value>avalible during application</param-value>
</context-param>
(2)servlet范围内的参数,只能在servlet的init()方法中取得,在web.xml中配置如下:
xml 代码
<servlet>
<servlet-name>MainServlet</servlet-name>
<servlet-class>com.wes.controller.MainServlet</servlet-class>
<init-param>
<param-name>param1</param-name>
<param-value>avalible in servlet init()</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
【web.xml】
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:conf/applicationContex*.xml</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:conf/log4j.properties</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>MainServlet</servlet-name>
<servlet-class>com.feihuale.test.MainServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation_init-param</param-name>
<param-value>WEB-INF/applicationContext-mvc.xml</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MainServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
根据上面的xml文件可以通过java代码获取
//----------------------------------------------------------
public class MainServlet extends HttpServlet {
public void init() throws ServletException {
System.out.print("contextConfigLocation参数是存放在servletcontext中的---->");
System.out.println(getServletContext().getInitParameter("contextConfigLocation"));
System.out.print("log4jConfigLoction参数是存放在servletcontext中的---->");
System.out.println(getServletContext().getInitParameter("log4jConfigLocation"));
System.out.print("contextConfigLocation_init-param参数是在servlet中存放的---->");
System.out.println(this.getInitParameter("contextConfigLocation_init-param"));
}
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
}
}
//----------------------------------------------------------
【4.常见的tomcat启动错误。】
在做struts+hibernate+spring项目时,启动程序后,发现错误 :
org.springframework.beans.factory.BeanDefinitionStoreException:
IOException parsing XML document from ServletContext resource
[/WEB-INF/applicationContext.xml]; nested exception is java.io.FileNotFoundException:
Could not open ServletContext resource [/WEB-INF/applicationContext.xml]
这是因为程序默认会在WEB-INF目录下查找applicationContext.xml文件,而现在程序找不到了,所以报错了。
解决方法有两个:
1. 把applicationContext.xml文件人工拷贝到WEB-INF目录下。
2. 在配置文件中指定applicationContext.xml文件的位置:修改web.xml文件,添加行:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContex*.xml</param-value>
</context-param >
//最后我还发现了一个问题<param-name>必须命名为contextConfigLocation,
连大小写也不能错,不信你试试。
推荐使用方法2。
参考地址:http://my.oschina.net/wxyplj/blog/14333
http://www.fansoo.com/blog/tag/applicationcontext-xml/