0

我正在尝试按照示例从数据库中实现为 JSF 应用程序加载资源包:internationalization in JSF with ResourceBundle entries which were loaded from database

对于测试,我编写了 getItSomehow() 代码,就像创建 HashMap 并用键“hello_world”和值“[”+locale+“]”+“hello world”填充它

当我在 Glassfish3 上部署该示例时,它运行良好。但是当我使用 WebSphere AS 7 时,jsf 页面只有第一次才能正确显示。在其他浏览器中打开 jsf 页面(选择其他首选语言)我总是在首次运行的语言环境中收到响应。

调试的时候发现ResourceBundle.java的实现有区别:Glassfish使用JDK1.6的rt.jar中提供的这个类;但是 WebSphere 在 java.util.jar 中有这个类

从 ApplicationResourceBundle.getResourceBundle() 调用的 ResourceBundle (WebSphere) 调用 handleGetBundle() 并最终调用 my.i18n.DbResourceBundle$DBControl.newBundle() 。使用不同的语言环境调用第二次(以及更多)时间,它不会调用我的覆盖,而只是返回为第一个语言环境创建的相同包。

问题:是否可以编写部署在 WebSphere AS 7.0.07 上的可内部化的 jsf Web 应用程序,而不是挖掘或侵入 AS 的内部?

(环境:Windows XP、WebSphere AS 7.0.0.7、jdk1.6.0_24、jsf 2.1.4)

4

1 回答 1

0

您可以提供 ResourceBundle 的特定实现。

这是一个每次 JSF 调用 ResourceBundle 方法时获取当前语言环境的示例:

package my.company.jsf.util;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;

import javax.faces.context.FacesContext;

public class MyBundle extends ResourceBundle {

    private static final Map<Locale, ResourceBundle> RB_CACHE = new HashMap<Locale, ResourceBundle>();
    private static final String BUNDLE_NAME = "my-messages";

    public MyBundle() {
    }

    @Override
    public Enumeration<String> getKeys() {
        ResourceBundle rb = getResourceBundle();
        final Iterator<String> it = rb.keySet().iterator();
        return new Enumeration<String>() {

            @Override
            public boolean hasMoreElements() {
                return it.hasNext();
            }

            @Override
            public String nextElement() {
                return it.next();
            }

        };
    }


    @Override
    protected Object handleGetObject(String key) {
        ResourceBundle rb = getResourceBundle();
        return rb.getObject(key);
    }

    private ResourceBundle getResourceBundle() {
        Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
        ResourceBundle rb = RB_CACHE.get(locale);
        if (rb == null) {
            rb = ResourceBundle.getBundle(BUNDLE_NAME, locale);
            RB_CACHE.put(locale, rb);
        }
        return rb;
    }
}

并在您的 faces-config.xml 中输入:

<?xml version="1.0" encoding="UTF-8"?>
<faces-config version="2.0" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd">
    <application>
        <resource-bundle>
            <base-name>my.company.jsf.util.MyBundle</base-name>
            <var>MSG</var>
        </resource-bundle>
    </application>
</faces-config>

我们遇到了同样的问题,该解决方案适用于 Windows Server 2008、WebSphere AS 7.0.0.19、jdk1.6.0_29、jsf 2.1.5

于 2012-04-03T14:24:07.153 回答