itVincent ,
非常感谢您的回复.看到您的回复,我就作了一下测试.
我的测试代码如下:
//InputStream in=LoginAction.class.getClassLoader().getResourceAsStream("com.yourcompany.struts.config.properties");
InputStream in=LoginAction.class.getClassLoader().getResourceAsStream("com//yourcompany//struts//config.properties");
System.out.println("properties in is : "+in);
Properties p=new Properties();
try {
p.load(in);
String h=p.getProperty("JAVA_HOME");
System.out.println("JAVA_HOME : -- "+h);
} catch (IOException e1) {
e1.printStackTrace();
}
我是在一个
action
中写的这段代码
.
执行后的结果如下:(错误)
properties in is : null
2006-10-24 20:36:59 org.apache.struts.action.RequestProcessor processExc
警告
: Unhandled Exception thrown: class java.lang.NullPointerException
页面的错误是:
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
javax.servlet.ServletException
org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:523)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:421)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
root cause
java.lang.NullPointerException
java.util.Properties$LineReader.readLine(Properties.java:365)
java.util.Properties.load(Properties.java:293)
com.yourcompany.struts.action.LoginAction.execute(LoginAction.java:134)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
note The full stack trace of the root cause is available in the Apache Tomcat/5.0.28 logs.
然后我又作了如下的测试:
InputStream in1=LoginAction.class.getClassLoader().getResourceAsStream("config.properties");
System.out.println("properties in is : "+in1);
Properties p1=new Properties();
try {
p1.load(in1);
String h1=p1.getProperty("JAVA_HOME");
System.out.println("JAVA_HOME : -- "+h1);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
执行后的结果如下:(正确)
properties in is : java.io.ByteArrayInputStream@257b34
JAVA_HOME : -- c:jdk
xml file url is : file:/D:/jakarta-tomcat-5.0.28/webapps/StrutsDemo/WEB-INF/
classes/config.xml
inputStream of the xml file is : java.io.BufferedInputStream@ca5bff
reader : org.dom4j.io.SAXReader@1cd3dd7
document doc is : org.dom4j.tree.DefaultDocument@13c4c09 [Document: name null
]
action document is : org.dom4j.tree.DefaultDocument@13c4c09 [Document: name
null]
note List is : 1
java home node is : org.dom4j.tree.DefaultElement@f1f34a [Element: <java att
ributes: []/>]
the value of the java node is : c:\jdk
两次的结果不同点的分析:
(主要看蓝色的代码部分,文件的存放位置的不同)
第一种方法,配置文件在
WEB-INF/classes/
com/yourcompany/struts/config.properties
目录下放着。也就是在包目录下。
第二种方法,配置文件在
WEB-INF/classes/
目录下存放。也就是根目录下。
所以如果用第二种方法来读第一种配置文件,就会出错。
--------------------------------------------------------------------------------------------------------------
以上是我的个人见解,欢迎批评指正.zhyiwww@163.com
|----------------------------------------------------------------------------------------|
版权声明 版权所有 @zhyiwww
引用请注明来源 http://www.blogjava.net/zhyiwww
|----------------------------------------------------------------------------------------|
posted on 2006-10-24 21:03
zhyiwww 阅读(3519)
评论(3) 编辑 收藏 所属分类:
discuss