覆盖 Struts 应用程序中的默认 ResourceBundle

发布于 2024-11-28 06:11:34 字数 940 浏览 0 评论 0原文

我有一个属性文件,它捆绑在 WEB-INF/lib 中的外部 jar 文件中。如果我想覆盖该属性文件中的值并使用我自己的属性文件中的值。有没有更好的方法来做到这一点。 例如: 在默认属性文件中,我看到

banner.ad.link=<a href="{1}" title="Click here {0}">{0}</a>

我想将其更改为类似以下内容:

banner.ad.link=<a class="mycss" href="{1}" title="Click here {0}">{0}</a>

到目前为止,这就是我所拥有的:

public class MainListener implements ServletContextListener {

    public void contextInitialized(ServletContextEvent event)
    {

        ResourceBundle bundle =  ResourceBundle.getBundle("com.comResources");


        bundle =  ResourceBundle.getBundle("org.displaytag.messages",
                Locale.getDefault());        
        for (Enumeration<String> e = bundle.getKeys(); e.hasMoreElements() ;) {
            System.out.println(e.nextElement());

        }
    }
}

我不太确定从这里到哪里去覆盖捆绑对象中的键值对以获得新的我自己的属性文件中的值。任何帮助,我将不胜感激!提前致谢!

I have a property file that is bundled in an external jar file in my WEB-INF/lib. If I want to override the values in that property file and use my own values from my own property file. Is there a better way to do it.
For example:
in the default property file, I see

banner.ad.link=<a href="{1}" title="Click here {0}">{0}</a>

I want to change it to something like:

banner.ad.link=<a class="mycss" href="{1}" title="Click here {0}">{0}</a>

So far this is what I have:

public class MainListener implements ServletContextListener {

    public void contextInitialized(ServletContextEvent event)
    {

        ResourceBundle bundle =  ResourceBundle.getBundle("com.comResources");


        bundle =  ResourceBundle.getBundle("org.displaytag.messages",
                Locale.getDefault());        
        for (Enumeration<String> e = bundle.getKeys(); e.hasMoreElements() ;) {
            System.out.println(e.nextElement());

        }
    }
}

I'm not too sure where to go from here to override the key value pair from the bundle object to have a new value from my own property file. Any help, I would be greatly appriciated! Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感受沵的脚步 2024-12-05 06:11:34

我看到了三种方法。

  1. 只需将属性文件放入类路径中,但位于要覆盖的属性文件之前。将其放在 WEB-INF/classes 中应该可以。
  2. 更改 jar 文件并从中删除属性文件
  3. 创建一个与属性文件具有相同基名的类,并委托给该类中的属性文件来实现捆绑包。请参阅 ResourceBundle api 文档 了解资源的使用方式捆绑包已加载。对于给定的包基本名称和区域设置,类文件将在属性文件之前加载。

请注意,如果您只想自定义 displaytag,则只需将 displaytag.properties 放在类路径的根目录中(即默认包中),而不是放在 org.displaytag.properties 包,如此处所述。如果找到,Displaytag 将加载您的捆绑包而不是默认捆绑包。

I see three ways of doing it.

  1. just put your properties file in the classpath, but before the properties file that you want to override. Putting it in WEB-INF/classes should work.
  2. Change the jar file and remove the properties file from it
  3. Create a class having the same basename as the properties file, and delegate to your properties file in the class to implement the bundle. See The ResourceBundle api doc to understand how resource bundles are loaded. The class file will be loaded before the properties file for a given bundle base name and locale.

Note that if all you want to do is to customize displaytag, you just have to put your displaytag.properties at the root of the classpath (i.e. in the default package) rather than in the org.displaytag.properties package, as documented here. Displaytag will load your bundle rather than the default one if found.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文