WebSphere 7 上 JSF 项目的内部化

发布于 2024-12-16 13:42:03 字数 904 浏览 2 评论 0原文

我正在尝试按照示例实现从数据库加载 JSF 应用程序的资源包: JSF 中的国际化,使用从数据库加载的 ResourceBundle 条目

对于测试,我编写了 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)

I'm trying to implement loading Resource Bundles for JSF application from DB, following the sample: internationalization in JSF with ResourceBundle entries which are loaded from database

For the test I coded getItSomehow() just as create HashMap and fill it with key "hello_world" and value "["+locale+"]"+"hello world"

The sample works fine when I deploy it on Glassfish3.
But when I use WebSphere AS 7, the jsf page is displayed correctly only for the first time. Opening the jsf page in other browsers (with other prefered language selected) I receive the respond always in the locale of first run.

While debugging, I found the difference in implementation of ResourceBundle.java: Glassfish uses this class provided in rt.jar of the JDK1.6; but WebSphere has this class inside java.util.jar

The ResourceBundle (of WebSphere) called from ApplicationResourceBundle.getResourceBundle() calls handleGetBundle() and finally invokes my.i18n.DbResourceBundle$DBControl.newBundle() .
Called second (and further) time with different locale it doesn't invoke my override but just returns the same bundle created for first locale.

The question: is it possible to code internalizable jsf web-application deployed on WebSphere AS 7.0.07, not digging nor hacking into internals of the AS?

(environment: Windows XP, WebSphere AS 7.0.0.7, jdk1.6.0_24, jsf 2.1.4)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流年里的时光 2024-12-23 13:42:03

您可以提供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

You can provide a specific implementation of ResourceBundle.

Here an example that gets the current locale each time JSF invokes the ResourceBundle methods:

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;
    }
}

and in your faces-config.xml put:

<?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>

We had your same problem and this solution worked for us with Windows Server 2008, WebSphere AS 7.0.0.19, jdk1.6.0_29, jsf 2.1.5

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文