读书人

struts2中自各儿定义的properties显示

发布时间: 2012-08-15 16:57:17 作者: rapoo

struts2中自己定义的properties显示消息的资源文件,读不出里面的vaule值
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值
不知道为什么

[解决办法]

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.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("操作完成");    }} 

读书人网 >Java Web开发

热点推荐