MessageResources

1.如果有多个.properties文件,那么struts用什么函数(或者配置)来选择读取哪个.properties文件呢?
2.在一般类中要读取.properties文件需要什么函数?

 

在struts-config.xml中可以指定多个resource,必须指明不同的key,没指明就使用默认的key

<message-resources parameter="ApplicationResources" />
<message-resources key="layout" parameter="Layout" />
使用的时候指定bundle属性为相应的配置中的key

<message key="hello.world"/> //取ApplicationResources.properties中的hello.world
<message key="hello.world" bundle=layout/> //取Layout.properties中的hello.world
回复人:deadsea(dr2tr) 2005-9-2 14:56:12
这是在jsp文件里吧,如果是在一个java工具类里呢(通过读取.properties文件获得数据库设置)
回复人:之支吾(super_zzw) 2005-9-2 15:05:13
MessageResourcesFactory factory = MessageResourcesFactory.createFactory();
resources = factory.createResources("application");//application.properties
resources.getMessage(key); //no parameter
resources.getMessage(key, arg1);//1 parameter
resources.getMessage(key, arg1,arg2);//2 parameter
resources.getMessage(key, arg1,arg2,arg3);//3 parameter
...
resources.getMessage(locale, key);
resources.getMessage(locale, key,arg1);
...
回复人: 一级(初级) user1 100 31269390 4245912 769444 you2004 0 2005-9-2 15:33:08 达达(dachun) 2005-9-2 16:03:18
干脆用多struts-config.xml,每个对应一个资源文件
回复人:deadsea(dr2tr) 2005-9-2 17:25:08
super_zzw(之支吾) : 谢谢
但是我按照你的方法做了,似乎还是不对
MessageResourcesFactory factory = MessageResourcesFactory.createFactory();
MessageResources resources = factory.createResources("ApplicationConfig");
String conn_str = resources.getMessage("db.conn_str");
//db.conn_str在Application.properties中定义,我单步调试发现这样写之后,conn_str的值还
//是null
struts-config.xml中定义如下:
<message-resources parameter="com.traffic.struts.ApplicationResources" />
<message-resources parameter="com.traffic.struts.ApplicationConfig" key="config"/>
不知道为什么?
回复人:之支吾(super_zzw) 2005-9-2 18:08:13
如果你的ApplicationConfig.properties文件在WEB-INF/classes目录下面应该可以的,当然,前提是在webapp中运行你的代码,而不是main函数里面测试你的代码。加载source的方式不一样的。如果你一定要在application的main函数里的取,只能自己去写加载ApplicationConfig.properties的方法了。
回复人:deadsea(dr2tr) 2005-9-3 9:47:10
谢谢super_zzw(之支吾)
果然是因为.properties文件目录不对才这样的
原因是我用MyEclipse做的时候,他的.properties文件是放在/src/com.yourcompanyname.struts里面的,不知道有什么设置可以改变目录呢(从classes改变到/src/com.yourcompanyname.struts) ?

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值