Pergunta

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>
Foi útil?

Solução

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.

Outras dicas

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>
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top