Overriding default ResourceBundle in Struts App
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!
I see three ways of doing it.
- 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.
- Change the jar file and remove the properties file from it
- 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.
精彩评论