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

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

问题描述

我希望能够从 JSF 2 托管 bean 内部的消息包中检索字符串.这将在字符串用作 FacesMessage 中的摘要或详细信息参数或用作抛出异常中的消息的情况下完成.

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.

推荐答案

可以通过Application#getMessageBundle().您可以通过 获取当前语言环境UIViewRoot#getLocale().您可以从完全限定的捆绑包中获取 ResourceBundleResourceBundle#getBundle().

总结:

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

<小时>

更新:根据问题中的错误,您实际上想要获得由 < 的 标识的包;资源包>.遗憾的是,标准 JSF API 不能直接使用此功能.您要么在代码中硬编码相同的基本名称并将上面示例中的 messageBundleName 替换为它,要么将其作为托管属性注入 <var> 在请求范围的 bean 中:


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天全站免登陆