Struts2 Struts 2覆盖资源消息键

Struts2 Struts 2覆盖资源消息键,struts2,resourcebundle,Struts2,Resourcebundle,想想这个 <constant name="struts.custom.i18n.resources" value="messages/default,messages/customize" /> 并且customize.properties内容是 label.sample = Default Sample //Duplicate key label.sample = Customize Sample 调用将导致自定义示例如果我们查看上述strust i18n,这

想想这个

<constant name="struts.custom.i18n.resources"
        value="messages/default,messages/customize" />
并且customize.properties内容是

label.sample = Default Sample
//Duplicate key
label.sample = Customize Sample
调用
将导致
自定义示例
如果我们查看上述strust i18n,这似乎是正确的行为,因为我们首先定义了默认值,然后是自定义值,因此自定义属性中的键将覆盖默认值中的键

现在,我们尝试动态覆盖定制消息。所以

   <!--The customize is removed -->
    <constant name="struts.custom.i18n.resources"
            value="messages/default" />
这行不通!作为替代,如果我们从i18n属性中删除默认值,并按如下操作,我们将获得自定义值

LocalizedTextUtil.clearDefaultResourceBundles();
LocalizedTextUtil.addDefaultResourceBundle("messages/default");
LocalizedTextUtil.addDefaultResourceBundle("messages/customize");
是否可以将默认属性列表保留为xml格式,并在运行时仅添加自定义属性


这就是我们需要它的原因 我们正在开发和托管一个web应用程序,该应用程序出售给许多客户。应用程序有一条默认消息。我们的一个客户可能希望更改某些应用程序默认消息,而其他客户则不希望。因此,我们有一个自定义邮件文件夹,让每家银行覆盖自己的邮件

我们为客户提供了以下文件夹结构:

+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
在StartUpSerlvet

//Set customer customize messages
LocalizedTextUtil.addDefaultResourceBundle("messages/" + activeCustomer+"/customize"); 

它不起作用,因为您的自定义
ServletContextListener
在S2从
struts.custom.i18n.resources
添加默认资源包之前运行

解决方案是从S2添加所有默认资源束后执行的地方执行
LocalizedTextUtil.addDefaultResourceBundle

例如,您可以扩展
strutspreadexecutefilter
,并在
postInit
方法中进行扩展。

为什么需要这个?你到底想做什么?亲爱的@AleksandrM请先查看我的更新如果你想添加资源,你不应该调用
clearDefaultResourceBundles
。您的
StartUpSerlvet
具体是什么以及它是如何配置的?对于
clearDefaultResourceBundles
您是对的。
StartUpSevlet
非常简单
ServletContextListener
从一些属性文件中获取活动客户,并调用
LocalizedTextUtil
。如果你觉得有帮助,我可以把代码放在这里!如果您使用
postInit
方法执行此操作,是否对您有效?您的顺序是正确的,使用
postInit
解决了此问题!
//Set customer customize messages
LocalizedTextUtil.addDefaultResourceBundle("messages/" + activeCustomer+"/customize");