简体   繁体   English

在faces-config jsf中调用多个属性,我总是调用资源吗?

[英]Invoking multiple properties in the faces-config jsf, I always invokes the resource?

I manage two properties is one for the generic messages and one for each module belonging to my system, but the problem I'm having is that when invoking the first method getMessage() always invokes the properties of the resource, when it should go the message. 我管理两个属性 ,一个属性用于通用消息,一个属性用于属于我系统的每个模块,但是我遇到的问题是,调用第一个方法时, getMessage()总是在调用资源时调用资源的属性。信息。

public class ResourceBundleUtil implements Serializable {

    private static final long serialVersionUID = 1L;

    public static final String MESSAGE_PATH = "messages";
    public static final String RESOURCE_PATH = "resources";
    private static HashMap<String, Object> messageBundles = new HashMap<String, Object>();

    public static String getMessage(String key) {
        if (key == null) {
            return null;
        }
        try {
            Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
            if (locale == null) {
                locale = Locale.ENGLISH;
            }
            ResourceBundle messages = (ResourceBundle) messageBundles.get(locale.toString());
            if (messages == null) {
                messages = ResourceBundle.getBundle(MESSAGE_PATH, locale);
                messageBundles.put(locale.toString(), messages);
            }
            return messages.getString(key);
        } catch (Exception e) {
            return key;
        }
    }

    public static String getResource(String key) {
        if (key == null) {
            return null;
        }
        try {
            Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
            if (locale == null) {
                locale = Locale.ENGLISH;
            }
            ResourceBundle messages = (ResourceBundle) messageBundles.get(locale.toString());
            if (messages == null) {
                messages = ResourceBundle.getBundle(RESOURCE_PATH, locale);
                messageBundles.put(locale.toString(), messages);
            }
            return messages.getString(key);
        } catch (Exception e) {
            return key;
        }
    }

}

The problem I have is that the getMessage() method always invokes the resource propertie, when should the message: 我的问题是, getMessage()方法总是在消息应何时调用资源属性:

if (messages == null) {
       messages = ResourceBundle.getBundle(MESSAGE_PATH, locale);
       messageBundles.put(locale.toString(), messages);
}

No enters the conditional, and noticed that comes with name value is resource . No输入条件,并且注意到名称值附带的是resource

Unlike the method getResource() if I notice that operating normally. 如果我注意到该方法正常运行,则与方法getResource()不同。

Please I could comment on that problem should, thanks. 请,我可以对此问题发表评论,谢谢。

Looks like the problem is that you store both kinds of bundles in single map. 看起来问题在于您将两种捆绑软件都存储在单个地图中。 So if resources bundle is used first, it is added to map. 因此,如果首先使用资源束,则将其添加到地图。 The key is current locale (eg "en"). 关键字是当前语言环境(例如“ en”)。

Then if you try to retrieve messages bundle with the same locale you in fact get resources bundle instead of messages one because you use the same key. 然后,如果您尝试使用相同的语言环境检索消息捆绑包,那么实际上您会获得资源捆绑包而不是消息捆绑包,因为您使用的是相同的密钥。

To solve this problem either use two separate maps or prepend key with bundle identifier like this: 要解决此问题,请使用两个单独的映射或带有包标识符的前缀键,如下所示:

messageBundles.put("resources_" + locale.toString(), messages);

Btw. 顺便说一句。 you use standard Java bundles. 您使用标准的Java捆绑软件。 More hints on how to use bundles defined in faces-config.xml can be found here: How can I get a message bundle string from inside a managed bean? 有关如何使用faces-config.xml中定义的包的更多提示,可以在这里找到: 如何从托管bean内部获取消息包字符串?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM