如何从托管bean中获取消息捆绑字符串? [英] How can I get a message bundle string from inside a managed bean?

查看:101
本文介绍了如何从托管bean中获取消息捆绑字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够从JSF 2受管Bean内部的消息束中检索字符串.在将字符串用作FacesMessage中的summary或​​details参数或在引发的异常中用作消息的情况下,将可以执行此操作.

I would like to be able to retrieve a string from a message bundle from inside a JSF 2 managed bean. This would be done in situations where the string is used as the summary or details parameter in a FacesMessage or as the message in a thrown exception.

我想确保托管bean为用户的语言环境加载正确的消息束.我不清楚如何使用JSF API调用从托管Bean中执行此操作.

I want to make sure that the managed bean loads the correct message bundle for the user's locale. It is not clear to me how to do this from a managed bean using JSF API calls.

我的配置是:

  • 使用Tomcat 7作为容器,因此解决方案不能依赖仅在完整的应用程序服务器容器中工作的API调用
  • 使用JSF 2参考实现(Mojarra)
  • 不使用任何允许CDI的库

注意::我确实看到了这个类似的问题 ,但这取决于我的配置中不可用的功能

NOTE: I did see this similar question, but it depends on features that are unavailable in my configuration

编辑:我在原始问题中犯了一个错误.我要问的是如何从托管bean中获取资源捆绑包字符串"? BalusC给了我正确答案.我实际上要问的问题的解决方案非常相似:

I made a mistake in my original question. What I meant to ask was "How can I get a resource bundle string from inside a managed bean"? BalusC gave me the correct answer for what I asked. The solution for what I actually meant to ask is very similar:

public static String getResourceBundleString(
            String resourceBundleName,
            String resourceBundleKey)
        throws MissingResourceException {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ResourceBundle bundle = 
        facesContext.getApplication().getResourceBundle(
            facesContext, resourceBundleName);
    return bundle.getString(resourceBundleKey);
}

此外,这是另一个问题的链接解释了消息"包和资源"包之间的区别.

Also, here is a link to another question that explains the difference between "message" bundles and "resource" bundles.

推荐答案

您可以通过获取当前的语言环境. UIViewRoot#getLocale() .您可以从完全合格的捆绑包中取出ResourceBundle 名称和语言环境,

You can get the full qualified bundle name of <message-bundle> by Application#getMessageBundle(). You can get the current locale by UIViewRoot#getLocale(). You can get a ResourceBundle out of a full qualified bundle name and the locale by ResourceBundle#getBundle().

所以,总结一下:

FacesContext facesContext = FacesContext.getCurrentInstance();
String messageBundleName = facesContext.getApplication().getMessageBundle();
Locale locale = facesContext.getViewRoot().getLocale();
ResourceBundle bundle = ResourceBundle.getBundle(messageBundleName, locale);
// ...


更新:根据问题中的错误,您实际上想获取由<resource-bundle><base-name>标识的捆绑包.遗憾的是,标准JSF API无法直接使用此功能.您必须在代码中对相同的基本名称进行硬编码,并用上面的示例替换messageBundleName,或者将其作为托管属性插入到请求范围内的bean中的<var>上:


Update: as per the mistake in the question, you actually want to get the bundle which is identified by the <base-name> of <resource-bundle>. This is unfortunately not directly available by a standard JSF API. You've either to hardcode the same base name in the code and substitute the messageBundleName in the above example with it, or to inject it as a managed property on <var> in a request scoped bean:

@ManagedProperty("#{msg}")
private ResourceBundle bundle; // +setter

这篇关于如何从托管bean中获取消息捆绑字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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