看了struts的actionservlet文件的源码 protected void initModuleMessageResources(ModuleConfig config)
        throws ServletException {        MessageResourcesConfig mrcs[] = config.findMessageResourcesConfigs();
        for (int i = 0; i < mrcs.length; i++) {
            if ((mrcs[i].getFactory() == null)
                || (mrcs[i].getParameter() == null)) {
                continue;
            }
            if (log.isDebugEnabled()) {
                log.debug(
                    "Initializing module path '"
                        + config.getPrefix()
                        + "' message resources from '"
                        + mrcs[i].getParameter()
                        + "'");
            }            String factory = mrcs[i].getFactory();
            MessageResourcesFactory.setFactoryClass(factory);
            MessageResourcesFactory factoryObject =
                MessageResourcesFactory.createFactory();
            factoryObject.setConfig(mrcs[i]);            MessageResources resources =
                factoryObject.createResources(mrcs[i].getParameter());
            resources.setReturnNull(mrcs[i].getNull());
            resources.setEscape(mrcs[i].isEscape());
            getServletContext().setAttribute(
                mrcs[i].getKey() + config.getPrefix(),
                resources);
        }    }
是不是要改写这个文件?

解决方案 »

  1.   

    Properties base = new Properties();
    InputStream is = null;
    is = getClass().getResourceAsStream("ApplicationResources_zh_CN.properties");
    base.load(is);
    然后通过base.getProperty("#####")就可以读取资源文件了
      不知道是不是你想要的
      

  2.   

    那如果我修改了ApplicationResources_zh_CN.properties以后,可以动态的加载它吗,就是说不重启,就可以用bean:message使用它吗?
      

  3.   

    不重新启动,那你肯定要deplay 重新编译了
      

  4.   

    to wfengxyy() 你这样得出来的is是null,我看了struts源码,读取方法你和差不多
      

  5.   

    this.getClass().getClassLoader().getResourceAsStream("ApplicationResources_zh_CN.properties");这样子就ok了
      

  6.   

    读取资源文件只是一部分
    现在已经实现了,我现在在后台动态添加一个资源文件,点一下鼠标就可以立马使用了得益于struts源码
      

  7.   

    发现文件变动后随时加载,没有变动不加载。
    import java.util.Properties;
    import java.io.File;
    import java.io.FileInputStream;
    public class JPropertiesSetting {    public static void main(String args[]) {
            new JPropertiesSetting();
        }    /**
         * 属性文件名
         */
    //  public static final String PFILE =
    //      "D:/TWorkspace/jmessage/classes/config.properties";    private static final String PFILE =
            "/opt/alert/jmessage/classes/config.properties";    /**
         * 对应属性文件对象变量
         */
        private File m_file = null;    /**
         * 属性文件的最后修改时间
         */
        private long m_lastModifiedTime = 0;    /**
         * 属性文件对应的属性对象变量
         */
        private Properties m_props = null;    /**
         * 本类可能存在的唯一个实例
         */
        private static JPropertiesSetting m_instance = new JPropertiesSetting();    /**
         * 私有构造函数.
         */
        private JPropertiesSetting() {
            this.m_file = new File(PFILE);
            this.m_lastModifiedTime = this.m_file.lastModified();
            if (this.m_lastModifiedTime == 0) {
                System.out.println(PFILE + " file not found ");
            }
            this.m_props = new Properties();
            try {
                m_props.load(new FileInputStream(PFILE));
            }
            catch (Exception e) {
                e.printStackTrace();
            }    }    /**
         * 静态工厂方法
         * @return ConfigManager 单例模式提供
         */
        synchronized public static JPropertiesSetting getInstance() {
            return m_instance;
        }    /**
         * 读取指定的属性项
         * @param pStrName String 属性的名称
         * @param pObjDefaultValue Object 默认值
         * @return Object
         */
        final public Object getItemValue(String pStrItemName,
                                         Object pObjDefaultValue) {
            long newTime = this.m_file.lastModified();
            //检查属性文件是否被更新过.如果被更新过那么重新获取最新版本.
            if (newTime == 0) {
                if (this.m_lastModifiedTime == 0) {
                    System.out.println(PFILE + " file does not exist !");
                }
                else {
                    System.out.println(PFILE + " file was deleted !");
                }
                return pObjDefaultValue;        }
            else if (newTime > this.m_lastModifiedTime) {
                this.m_props.clear();
                try {
                    m_props.load(new FileInputStream(PFILE));
                }
                catch (Exception e) {
                    e.printStackTrace();
                }        }
            this.m_lastModifiedTime = newTime;
            Object val = this.m_props.getProperty(pStrItemName);
            if (val == null) {
                return pObjDefaultValue;
            }
            else {
                return val;
            }    }    /**
         * 读取指定的属性项
         * @param pItemName String
         * @param value String
         */
        final public Object getItemValue(String pItemName) {        return this.getItemValue(pItemName, null);
        }    /**
         * 设置配置项名称及其值
         * @param pItemName String
         * @param value String
         */
        final public void setItemValue(String pItemName, String value) {
            this.m_props.setProperty(pItemName, value);
            return;
        }}
      

  8.   

    to   rubyjn() ( ) 信誉:100    Blog 
    是不是只能运行一次阿,在什么地方循环?
      

  9.   

    我做的是修改struts的语言资源文件,并且能动态加载
    看了struts源码以后,我 已经实现了,哈哈
    比如你在ApplicationResources_zh_CN.properties中新加了一个aaa=test只要一刷新,就可以在jsp中立马使用<bean:message key="aaa"/>了多看源码,很有帮助