Struts 2 覆盖资源消息键 [英] Struts 2 override resource messages keys

查看:32
本文介绍了Struts 2 覆盖资源消息键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一下

<constant name="struts.custom.i18n.resources"
        value="messages/default,messages/customize" />

default.properties内容是

label.sample = Default Sample

customize.properties 的内容是

And the customize.properties content is

//Duplicate key
label.sample = Customize Sample

调用 <s:text name="label.sample"> 将导致 Customize Sample 如果我们查看上述 strust i18n,这似乎是正确的行为,因为我们首先定义默认值,然后自定义,因此自定义属性中的键将覆盖默认键.

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" />

在一些像启动 servlet 的地方,我们添加自定义消息:

In some where like an startup servlet, we add customize messages as:

LocalizedTextUtil.clearDefaultResourceBundles();
LocalizedTextUtil.addDefaultResourceBundle("messages/customize");

这行不通!作为替代,如果我们从 i18n 属性中删除默认值并按如下操作,我们将获得自定义值

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

是否可以在 xml 中保留默认属性列表,并在运行时仅添加自定义属性

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

在 StartUpSerlvet 中

And in StartUpSerlvet

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

推荐答案

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

It doesn't work because your custom ServletContextListener runs before S2 adds default resource bundles from struts.custom.i18n.resources.

解决方案是从 S2 添加所有默认资源包后执行的某处执行 LocalizedTextUtil.addDefaultResourceBundle.例如,您可以扩展 StrutsPrepareAndExecuteFilter 并在 postInit 方法中进行.

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.

这篇关于Struts 2 覆盖资源消息键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆