用getBundle()说找不到资源文件

hevil 2003-07-29 01:03:14
我在jsp里调用了一个Servlet(test.class),Servlet里用了一个 ResourceBundle.getBundle()方法,但是报错说找不到对应的资源文件。
请问路径该如何设置。
我的配置:WLS 7.0

Myapp
_________|___________
| |
Test.jsp WEB-INF
__________|___________
| | |
web.xml lib classes
| |
test.class Login.properties

Servlet的方法:
public static Properties getPropertiesFromBundle(String s, ServletContext servletcontext)
throws Exception
{
System.out.println("Looking for bundle " + s);
Properties properties = new Properties();
int i;
if((i = s.indexOf(".properties")) >= 0)
{
s = s.substring(0, i);
}
ResourceBundle resourcebundle = ResourceBundle.getBundle(s);
String s1;
String s3;
for(Enumeration enumeration = resourcebundle.getKeys(); enumeration.hasMoreElements(); )
{
s1 = (String)enumeration.nextElement();
s3 = resourcebundle.getString(s1);
properties.put(s1, s3);
}

if(servletcontext != null)
{
String s2;
for(Enumeration enumeration1 = servletcontext.getInitParameterNames(); enumeration1.hasMoreElements(); properties.put(s2, servletcontext.getInitParameter(s2)))
{
s2 = (String)enumeration1.nextElement();
System.out.println("Key: " + s2 + " ==> " + servletcontext.getInitParameter(s2));
}

}
return properties;
}

jsp 里调用:
.........
loginProperties = test.getPropertiesFromBundle("Login",application);

我试过方法没有问题,因为我把这个方法直接拷到jsp页面不调用Servlet时可以成功,但是如果是通过调用Servlet的就不行,好奇怪。
...全文
123 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
hevil 2003-07-29
  • 打赏
  • 举报
回复
试过了,都不行啊。可以确认的是,Servlet 是调用成功的,除了这个方法不行;Servlet里的方法移到Test.jsp,直接用loginProperties = getPropertiesFromBundle("Login",application)也是成功的,路径就是我原来的。纳闷的是在Servlet中调用就不行。


chulj 2003-07-29
  • 打赏
  • 举报
回复
由于ResourceBundle在查找文件时,在运行此class的目录下,因此在发布时,需要将.properties文件发布到class所在的目录。
baxp 2003-07-29
  • 打赏
  • 举报
回复
Myapp
_________|___________
| |
Test.jsp WEB-INF
__________|_________________
| | |
web.xml lib ____classes____
| |
test.class Login.properties
javahui 2003-07-29
  • 打赏
  • 举报
回复
Myapp
_________|___________
| |
Test.jsp WEB-INF
__________|___________
| | |
web.xml lib classes
| |
test.class Login.properties

你的这个结构图做的挺不错的,能告诉我怎么做的吗?

我建议你改成下面的试一试:
Myapp
_________|___________
| |
Test.jsp WEB-INF
__________|________________________
| | | |
web.xml lib classes Login.properties
|
test.class
cqnieenp 2003-07-29
  • 打赏
  • 举报
回复
你把Login.properties 方到lib下看呢?

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧