我自定义了一个实现了ServletContextListener接口的监听器(MyListener),
读取xml文件。
web.xml的配置如下
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>config/test.xml</param-value>
</context-param>
<listener>
<listener-class>com.lyq.action.MyListener</listener-class>
</listener>其中config/test.xml在classpath即src下可是在MyListener中为什么取得路径是错误的呢。source如下:
String configurationLocation = context.getInitParameter("contextConfigLocation");
SAXReader reader = new SAXReader();
Document doc = reader.read(configurationLocation); //用dom4j读取错误如下:
D:\MyLucy\eclipse\config\test.xml (系统找不到指定的路径。) Nested exception: D:\MyLucy\eclipse\config\test.xml (系统找不到指定的路径。)请那为大人告诉我一下,如何取得正确的路径名:
例如:e:\work\workspace\testPJ\src\config\test.xml谢谢!!!!!
读取xml文件。
web.xml的配置如下
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>config/test.xml</param-value>
</context-param>
<listener>
<listener-class>com.lyq.action.MyListener</listener-class>
</listener>其中config/test.xml在classpath即src下可是在MyListener中为什么取得路径是错误的呢。source如下:
String configurationLocation = context.getInitParameter("contextConfigLocation");
SAXReader reader = new SAXReader();
Document doc = reader.read(configurationLocation); //用dom4j读取错误如下:
D:\MyLucy\eclipse\config\test.xml (系统找不到指定的路径。) Nested exception: D:\MyLucy\eclipse\config\test.xml (系统找不到指定的路径。)请那为大人告诉我一下,如何取得正确的路径名:
例如:e:\work\workspace\testPJ\src\config\test.xml谢谢!!!!!
<param-value>classpath*:config/test.xml</param-value>
</context-param>
org.dom4j.DocumentException: no protocol: classpath*:config/test.xml Nested exception: no protocol: classpath*:config/test.xml
加了还是不行。回报这个错:org.dom4j.DocumentException: Error on line 1 of document : Content is not allowed in prolog. Nested exception: Content is not allowed in prolog.
at org.dom4j.io.SAXReader.read(SAXReader.java:482)
at org.dom4j.io.SAXReader.read(SAXReader.java:343)
at com.lyq.action.MyListener.contextInitialized(MyListener.java:47)
test.xml<?xml version="1.0" encoding="UTF-8"?>
<student>
<person id="0001" sex="男">
<name>张三</name>
<age>25</age>
<college>工商管理学院</college>
<telephone>6666666</telephone>
</person>
<person id="0002" sex="女">
<name>李四</name>
<age>26</age>
<college>医学院</college>
<telephone>8888888</telephone>
</person>
<person id="0003">
<name>王五</name>
<age>26</age>
<college>医学院</college>
<telephone>8888888</telephone>
</person>
</student>
<init-param>
<param-name>configPath</param-name>
<param-value>WEB-INF/classes/config.properties</param-value>
</init-param>类似这样
WEB-INF加上
http://blog.csdn.net/kingdizzy/article/details/5157865