Consider this
<constant name="struts.custom.i18n.resources"
value="messages/default,messages/customize" />
And default.properties
content is
label.sample = Default Sample
And the customize.properties content is
//Duplicate key
label.sample = Customize Sample
Calling <s:text name="label.sample">
will result in Customize Sample
If we review the above strust i18n this seems correct behavior as we first defined default and then the customize, so the keys in customize properties will override the keys in default.
Now we try to override the customize messages dynamically. So
<!--The customize is removed -->
<constant name="struts.custom.i18n.resources"
value="messages/default" />
In some where like an startup servlet, we add customize messages as:
LocalizedTextUtil.clearDefaultResourceBundles();
LocalizedTextUtil.addDefaultResourceBundle("messages/customize");
This will not work! As alternative if we remove default from i18n property and do it as below, we will get the customize value
LocalizedTextUtil.clearDefaultResourceBundles();
LocalizedTextUtil.addDefaultResourceBundle("messages/default");
LocalizedTextUtil.addDefaultResourceBundle("messages/customize");
Is it possible to keep the list of default properties in xml and only add customize ones at run time
This why we need it We are developing AND hosting a web application which is sold to lots of customers. The application has a default message. It is possible that ONE of our customers wants to change some of application default messages while the others not. So we have a folder of customize messages and let each bank override its own messages.
We have below folder structure for customers:
+messages
-resources_fa_IR.properties
-resources_en_US.properties
+customer1
-customize_fa_IR.properties
-customize_en_US.properties
+customer2
-customize_fa_IR.properties
-customize_en_US.properties
And in StartUpSerlvet
//Set customer customize messages
LocalizedTextUtil.addDefaultResourceBundle("messages/" + activeCustomer+"/customize");
It doesn't work because your custom ServletContextListener
runs before S2 adds default resource bundles from struts.custom.i18n.resources
.
The solution is to execute LocalizedTextUtil.addDefaultResourceBundle
from somewhere what is executed after the S2 added all default resource bundles.
For example you can extend StrutsPrepareAndExecuteFilter
and do it in postInit
method.
来源:https://stackoverflow.com/questions/28137603/struts-2-override-resource-messages-keys