struts2中自己定义的properties显示消息的资源文件,读不出里面的vaule值

q539816590 2012-07-02 11:18:16
struts2中自己定义的properties显示消息的资源文件,读不出里面的vaule值

我的properties文件时放在webinf下面的,所有配置都放在这里面,都在web.xml里面配置好了,怎么没用,用gettext方法取不出信息,用fielderror标签只是显示配置文件的key名字,不显示value值

web.xml的部分配置
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
<init-param>
<param-name>config</param-name>
<param-value>struts-default.xml,struts-plugin.xml,../config/struts/struts.xml</param-value>
</init-param>
</filter>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

struts.xml的部分配置
<constant name="spring.ObjectFactory" value="spring"></constant>
<constant name="struts.custom.i18n.resources" value="/WEB-INF/config/struts/messages-error"></constant>

messages-error.properties文件配置,用于显示消息
# roleProfile validation #
error.roleProfile.required=roleId is required.
error.module.required=moduleId is required.

existed.roleId=Role ID {0} has existed;

create.role=Create New Role {0}.
create.fail=Create New Role {0} Fail due to an exception with ticket number {1}.
create.successFully=Create New Role {0} Successfully.

delete.role=Roles {0} selected for deletion.
delete.fail=Roles {0} set for deletion fail.
delete.successFully=Roles {0} set for deletion successfully.

update.role=Start update Role {0}.
update.fail=Update Role {0} Fail due to an exception with ticket number {1}.
update.successFully=Update Role {0} Successfully.

action中的代码
public String saveRolePromfile() throws Exception {
moduleList=moduleBiz.getModule();
System.out.println(this.getText("existed.roleId", new String []{roleProfile.getRoleId()}));
if(roleProfile.getRoleId().trim().equals("") || roleProfile.getRoleId()=="")
{
this.addFieldError("error.roleProfile.required","error.roleProfile.required" );
return ERROR;
}
if(moduleId.trim().equals("") || moduleId=="")
{
this.addFieldError("error.module.required", "error.module.required");
return ERROR;
}
boolean flag= roleProfilebiz.getRoleProfileExist(roleProfile.getRoleId());
if(flag)
{
this.addFieldError("existed.roleId",
this.getText("existed.roleId", new String []{roleProfile.getRoleId()}));
return ERROR;
}
System.out.println(moduleId);
String [] mid=moduleId.split(",");
List<RoleModule> roleModule=new ArrayList<RoleModule>();
for (int i = 0; i < mid.length; i++) {
RoleModule rm=new RoleModule();
rm.setModuleId(mid[i]);
roleModule.add(rm);
}
System.out.println(roleProfile.getRoleId());

System.out.println(roleProfile.getRoleId());
flag=roleProfilebiz.addRoleProfile(roleProfile, roleModule);
if(flag)
return SUCCESS;
else
return ERROR;

}

jsp页面的标签

<s:fielderror></s:fielderror>


标签只显示文件的key值
不知道为什么
...全文
369 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
清澈@Cherry 2013-03-03
  • 打赏
  • 举报
回复
我怀疑你的struts.xml文件有两个<constant name="struts.custom.i18n.resources" value="Point3D"></constant> ,因为我就遇到这样的问题,一个节点就有用,两个就没用
zhangmingqiang 2012-12-05
  • 打赏
  • 举报
回复
问题解决了吗?
q539816590 2012-07-02
  • 打赏
  • 举报
回复
坐等啊,怎么没用大神帮助
q539816590 2012-07-02
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]
Java code


import java.util.Locale;
import java.util.ResourceBundle;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io……
[/Quote]这样可以吗?struts2不是可以自动加载properties文件吗,我这个路径换了,没办法加载吗?
q539816590 2012-07-02
  • 打赏
  • 举报
回复
[Quote=引用楼主 的回复:]
struts2中自己定义的properties显示消息的资源文件,读不出里面的vaule值

我的properties文件时放在webinf下面的,所有配置都放在这里面,都在web.xml里面配置好了,怎么没用,用gettext方法取不出信息,用fielderror标签只是显示配置文件的key名字,不显示value值

web.xml的部分配置
<filter>
<filter-n……
[/Quote]这样可以吗?struts2不是可以自动加载properties文件吗,我这个路径换了,没办法加载吗?
暗夜螃蟹 2012-07-02
  • 打赏
  • 举报
回复

import java.util.Locale;
import java.util.ResourceBundle;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

/**
* @author 螃蟹
*/
public class GetProperties {

private ResourceBundle bundle = null;

private String proName = "";

public GetProperties(String proname) {
this.proName = proname;
}

/**
* 按照KEY获取配置文件中相应值
*
* @param type
* @return 取其中的属性值(String类型)
*/
public String getProp(String type) throws Exception {
if (bundle == null) {
try {
bundle = ResourceBundle.getBundle(this.proName, Locale.getDefault());
} catch (Exception e) {
System.out.println("获取资源出错!" + e);
}
}
return bundle.getString(type);
}

// 属性文件的路径

String profilepath = Thread.currentThread().getContextClassLoader().getResource("dictionary.properties").toString();

public Properties props = new Properties();

public String init(String str, String val) {

try {

if (profilepath.indexOf("file:") != -1) {
profilepath = profilepath.replaceFirst("file:", "");
}

props.load(new FileInputStream(profilepath));
} catch (FileNotFoundException e) {
e.printStackTrace();
System.exit(-1);
} catch (IOException e) {
System.exit(-1);
}
return updateProperties(str, val);
}

/**
* 更新properties文件的键值对 如果该主键已经存在,更新该主键的值; 如果该主键不存在,则插件一对键值。
*
* @param keyname
* 键名
* @param keyvalue
* 键值
*/
public String updateProperties(String keyname, String keyvalue) {
try {

if (profilepath.indexOf("file:") != -1) {
profilepath = profilepath.replaceFirst("file:", "");
}

props.load(new FileInputStream(profilepath));
// 调用 Hashtable 的方法 put,使用 getProperty 方法提供并行性。
// 强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
OutputStream fos = new FileOutputStream(profilepath);
props.setProperty(keyname, keyvalue);
// 以适合使用 load 方法加载到 Properties 表中的格式,
// 将此 Properties 表中的属性列表(键和元素对)写入输出流
props.store(fos, "Update '" + keyname + "' value");
} catch (IOException e) {
System.err.println("属性文件更新错误");
}
return "success";
}

// 测试代码
public static void main(String[] args) {
// /dictionary.properties
// String s=System.getProperty("user.dir");
// System.out.println("==>"+Thread.currentThread().getContextClassLoader().getResource("dictionary.properties"));
// gp.updateProperties("test", "tanglong8848");
// System.out.println("操作完成");
}

}

81,091

社区成员

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

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