定制jsf资源包处理程序时出错

时间:2019-03-10 05:36:10

标签: jsf jsf-2

在一个项目中,我将自定义jsf资源包处理程序。根据这篇文章:
i18n with UTF-8 encoded properties files in JSF 2.0 application

我在我的faces-config中添加以下行:

<application>
    <locale-config>
        <default-locale>fa</default-locale>
        <supported-locale>en</supported-locale>
        <supported-locale>fa</supported-locale>
    </locale-config>
    <message-bundle>ApplicationResources</message-bundle>
    <resource-bundle>
        <base-name>org.apache.myfaces.bundle.CustomJsfBundleHandler</base-name>
        <var>messages</var>
    </resource-bundle>
</application>

并创建处理程序:

package org.apache.myfaces.bundle;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.PropertyResourceBundle;

public class CustomJsfBundleHandler extends PropertyResourceBundle {

    public CustomJsfBundleHandler(InputStream stream) throws IOException {
        super(stream);
    }

    public CustomJsfBundleHandler(Reader reader) throws IOException {
        super(reader);
    }

    @Override
    public Object handleGetObject(String key) {
       // do some customization
       return super.handleGetObject(key);
    }
}  

但是当进入我的页面时,出现以下异常:

  

java.util.MissingResourceException:找不到基本名称为org.apache.myfaces.bundle.CustomJsfBundleHandler的包,语言环境en       在java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:1564)       在java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1387)       在java.util.ResourceBundle.getBundle(ResourceBundle.java:1082)       在org.apache.myfaces.application.ApplicationImpl.getResourceBundle(ApplicationImpl.java:459)       在org.apache.myfaces.application.ApplicationImpl.getResourceBundle(ApplicationImpl.java:435)       在org.apache.myfaces.el.unified.resolver.ResourceBundleResolver.getResourceBundle(ResourceBundleResolver.java:222)       在org.apache.myfaces.el.unified.resolver.ResourceBundleResolver.getValue(ResourceBundleResolver.java:136)

你有什么主意吗?

2 个答案:

答案 0 :(得分:0)

据我了解,到目前为止,您的CustomJsfBundleHandler做得还不够。确保在messages.properties文件夹下的org/apache/myfaces/bundle/CustomJsfBundleHandler文件夹中有一个src/main/resources

答案 1 :(得分:0)

如果您仍然需要答案:我今天也遇到了同样的问题,解决方案如下:您必须类似于此线程中提到的解决方案来实现它:https://stackoverflow.com/a/3646601/6650315

在您提到的示例中,自定义资源束的完全分类的类名称为org.apache.myfaces.bundle.CustomJsfBundleHandler,已在faces-config.xml中正确设置为基本名称。在文件夹src/main/resources内,必须创建目录结构org/apache/myfaces/bundle,在最后一个目录内,必须创建名为customJsfBundleHandler.properties(和customJsfBundleHandler_de.properties ...)的文件。因此,完整的文件路径应为src/main/resources/org/apache/myfaces/bundle/customJsfBundleHandler.properties

所以结论是:

  • 您的自定义捆绑软件实现的完全分类的类名定义了必须在faces-config.xml中提及的基本名称。
  • 完全分类的类名(基本名称)定义了src/main/resources内的完整目录结构
  • 简单的类名定义了用camelCase编写的属性文件的名称