jsf - 如何本地化 JSF 复合组件?

标签 jsf localization composite-component

我在本地化复合组件(Mojarra 2.2、Glassfish 4.0)时遇到问题。

我尝试了BalusC的解决方案here将属性文件放在与复合组件相同的目录中,并具有与复合组件相同的文件名和 .properties扩展——用于将复合组件定义中的字符串外部化。

但是当我想要本地化字符串(通过在属性文件的文件名中添加 _en 后缀)时,我收到此错误(唯一更改的是属性文件的名称):

[2013-08-08T13:49:04.234-0500] [glassfish 4.0] [WARNING] [] [javax.enterprise.web] [tid: _ThreadID=24 _ThreadName=http-listener-1(5)] [timeMillis: 1375987744234] [levelValue: 900] [[
  StandardWrapperValve[Faces Servlet]: Servlet.service() for servlet Faces Servlet threw exception
java.lang.NullPointerException
at javax.faces.component.UIComponent.findComponentResourceBundleLocaleMatch(UIComponent.java:1204)
at javax.faces.component.UIComponent.getResourceBundleMap(UIComponent.java:1007)
at sun.reflect.GeneratedMethodAccessor1226.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at javax.el.BeanELResolver.getValue(BeanELResolver.java:363)
at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
at com.sun.el.parser.AstValue.getValue(AstValue.java:140)
at com.sun.el.parser.AstValue.getValue(AstValue.java:204)
at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:226)
at org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:50)
at com.sun.faces.facelets.el.ELText$ELTextVariable.writeText(ELText.java:227)
at com.sun.faces.facelets.compiler.TextInstruction.write(TextInstruction.java:85)
at com.sun.faces.facelets.compiler.UIInstructions.encodeBegin(UIInstructions.java:82)
at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:302)
at com.sun.faces.renderkit.html_basic.GroupRenderer.encodeChildren(GroupRenderer.java:115)
at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:894)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1856)

...

我读了一篇article (发布于 2012 年 8 月 24 日下午 3:50)JSF 的本地化与标准 Java 不同,并且属性文件将位于 resources/en/<library>/<component>.properties 中。 ,所以我输入 mycomp.properties在那里,再次出现错误。

我的默认区域设置实际上是“en_US”——我也尝试过,得到了相同的结果。

所以,这有效:
resources/lib/mycomp.xhtml , resources/lib/mycomp.properties

这些不起作用:
resources/lib/mycomp.xhtml , resources/lib/mycomp_en.properties
resources/lib/mycomp.xhtml , resources/en/lib/mycomp.properties

我做错了什么?

最佳答案

我可以重现你的问题。这实在是太尴尬了。显然,在较新的 Mojarra 版本中,与此相关的一些内容发生了变化。我无法在 2.2.0 中同时使用旧方法和新方法。

我再次查看了 Mojarra 源代码,从 UIComponent#getResourceBundleMap() 开始,我了解到首先根据组件类的 FQN 搜索资源包。我们可以轻松地利用它。

如果尚未完成,请首先创建一个支持组件类:

package com.example.composite;

@FacesComponent("myComposite")
public class MyComposite extends UINamingContainer {
    // Can be kept empty.
}

并在复合组件定义中声明它:

<cc:interface componentType="myComposite">

然后,您可以简单地将 MyComposite.propertiesMyComposite_en.properties 等放入同一个 com.example.composite 包中。它对我有用。

关于jsf - 如何本地化 JSF 复合组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18134378/

相关文章:

jsf - com.sun.faces.context.FacesContextImpl.assertNotReleased 处的 java.lang.IllegalStateException

android - Android Studio 中未启用翻译编辑器

ios - xCode 不会列出 Storyboard或 info.plist 以进行本地化

jsf - 获取复合组件本身的客户端 ID 以在 JavaScript 中使用

javascript - 在 JSF 复合组件中集成 JavaScript,干净的方式

jsf - 在 JSF 中,有没有一种方法可以根据条件或参数向组件添加 passThroughAttribute?

java - JSF 验证用户状态

Java字符串搜索忽略重音

jsf - 如何创建一个在 inputText 和 inputSecret 之间切换的复合组件?

java - 将 JPQL 自定义查询结果渲染到 JSF 页面中的问题