开发者

JSF 1.2 Application.getMessageBundle() returns null

开发者 https://www.devze.com 2023-01-28 04:33 出处:网络
I\'m using Spring 2.5 with JSF 1.2, on Tomcat 6.0.13. In one part of code, I\'m trying to load ResourceBundle by using following approach:

I'm using Spring 2.5 with JSF 1.2, on Tomcat 6.0.13.

In one part of code, I'm trying to load ResourceBundle by using following approach:

ResourceBundle.getBundle(context.getApplication().getMessageBundle(), Locale.EN);

The problem is that getMessageBundle() method returns null. This used to work with JSF 1.1. Does anybody have idea what could be the problem?

For now I'm going to hardcode bundle name, but I would prefer if all my configuration data will be placed inside faces-config.

Resource bundle is set as following:

    <application>
        <locale-config>
            <default-locale>en</default-locale>
        </locale-config>
        <resource-bundle>
            <base-name>org.mysite.MessageBundle</base-name>
            <var>msgs</var>
        </resource-bundle>
    </application>
开发者_StackOverflow中文版


IgorB,

You may be able to use resource injection to have JSF provide your managed bean with the correct ResourceBundle. This would remove the need to hard-code anything in your Java source and keep the association nicely centralized.

Start by defining a managed property on your backing bean. In the JSF configuration, set the managed property's value to an EL expression that references your resource bundle.

I've done something like the following using Tomcat 6. The only caveat is that you can't access this value from your backing bean's constructor, since JSF will not yet have initialized it. Use @PostConstruct on an initialization method if the value is needed early in the bean's lifecycle.

<managed-bean>
  ...
  <managed-property>
    <property-name>messages</property-name>
    <property-class>java.util.ResourceBundle</property-class>
    <value>#{msg}</value>
  </managed-property>
  ...
</managed-bean>

<application>
  ...
  <resource-bundle>
    <base-name>application_messages</base-name>
    <var>msg</var>
  </resource-bundle>
  ...
</application>


The getMessageBundle() returns the value of <message-bundle> entry in faces-config.xml, not the <resource-bundle> entry.

Its value is actually not avaliable by the JSF 1.2 API. You have to specify it yourself.

ResourceBundle bundle = context.getApplication().getResourceBundle(context, "org.mysite.MessageBundle");

The <message-bundle> is for validation/conversion messages. Probably you've actually used this in JSF 1.1.

0

精彩评论

暂无评论...
验证码 换一张
取 消