Struts 2 override resource messages keys

岁酱吖の 提交于 2019-12-02 12:43:17

问题


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"); 

回答1:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!