如何使用 JSF 查看 Jar 中的 message.properties?
使用:JSF 1.2、Facelets 1.1.15、GateIn 3.1 GA、Richfaces 3.3.3
我在 JAR 中有一些常见的 .xhtml
和支持 bean,我们的 portlet 可以看到。我通过重写 ResourceResolver 来做到这一点,如其他帖子中所述:
Portlet 可以加载 XHTML 并使用支持 bean。
这是我的问题:我无法使用 xhtml 来替换 messages_en.properties
中定义的消息。我尝试将属性文件移到 JAR 之外并直接放入 /lib
文件夹中。我还尝试在名称前面放置 /
来尝试让解析器找到它。我也把它放在组件文件夹中。
常见的 jar 信息是:我有一个 my-portlet-common-resources.jar
,它驻留在 server/my-portal/lib
中。 jar 的结构如下:
- com/portlet/common/CustomResourceResolver.class
- com/portlet/common/FilterCreateBean.class - 公共弹出消息的支持 bean
- messages_en.properties
- faces-config.xml
- META-INF/components/commonPopups.xhtml
- META -INF/faces-config.xml - 声明 FilterBean
- META-INF/Manifest.mf
faces-config.xml
内容:
<?xml version="1.0" encoding="UTF-8"?>
<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
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_1_2.xsd"
version="1.2">
<application>
<locale-config>
<default-locale>en</default-locale>
</locale-config>
<message-bundle>/messages_en.properties</message-bundle>
</application>
<managed-bean>
<managed-bean-name>FilterCreateBean</managed-bean-name>
<managed-bean-class>com.portlet.common.FilterCreateBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
</faces-config>
将消息包含在 commonPopups.xhtml
中(部分片段):
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:c="http://java.sun.com/jstl/core"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich">
<a4j:loadBundle basename="messages" var="msgs"/>
<rich:panel style="border-style:none;" id="addNewChainPanel">
<rich:modalPanel id="modalNewChainPanel" autosized="true">
<f:facet name="header"><h:outputText value="#{msgs['filterset.modal.new.title']}" /></f:facet>
</ui:composition>
Using: JSF 1.2, Facelets 1.1.15, GateIn 3.1 GA, Richfaces 3.3.3
I have some common .xhtml
and backing bean in a JAR that our portlets can see. I did this by overriding ResourceResolver as described in other posts:
- http://ocpsoft.com/opensource/create-common-facelets-jar/
- How to use Facelets composition with files from another context
The portlets can load the XHTML and use the backing bean.
Here is my problem: I cannot get the xhtml to substitute the messages defined in messages_en.properties
. I have tried moving the properties file outside of the JAR and placing directly in /lib
folder. I have also tried putting a /
in front of the name to try to get the resolver to find it. I have also put it in the components folder.
The common jar info is: I hava a my-portlet-common-resources.jar
which resides in server/my-portal/lib
. The jar is structured like so:
- com/portlet/common/CustomResourceResolver.class
- com/portlet/common/FilterCreateBean.class - backing bean for the common popup
- messages_en.properties
- faces-config.xml
- META-INF/components/commonPopups.xhtml
- META-INF/faces-config.xml - declares the FilterBean
- META-INF/Manifest.mf
faces-config.xml
contents:
<?xml version="1.0" encoding="UTF-8"?>
<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
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_1_2.xsd"
version="1.2">
<application>
<locale-config>
<default-locale>en</default-locale>
</locale-config>
<message-bundle>/messages_en.properties</message-bundle>
</application>
<managed-bean>
<managed-bean-name>FilterCreateBean</managed-bean-name>
<managed-bean-class>com.portlet.common.FilterCreateBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
</faces-config>
Include the messages in commonPopups.xhtml
(partial snip):
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:c="http://java.sun.com/jstl/core"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich">
<a4j:loadBundle basename="messages" var="msgs"/>
<rich:panel style="border-style:none;" id="addNewChainPanel">
<rich:modalPanel id="modalNewChainPanel" autosized="true">
<f:facet name="header"><h:outputText value="#{msgs['filterset.modal.new.title']}" /></f:facet>
</ui:composition>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这应该有效。也许您的主 Web 应用程序的类路径根目录中已经有一个
messages*.properties
文件。这个在类加载中具有优先权。您需要将其放入更具体的包中。将 JAR 放入例如com/portlet/common
文件夹中,以便它成为com.portlet.common
包的成员。这样,它就可以通过以下方式获得:与具体问题无关,
faces-config.xml
中的
条目有一个完全不同的目的。它应该覆盖由 JSF 默认验证器/转换器返回的 JSF 默认验证/转换消息。它无意提供本地化内容。您可以在此处使用
条目或
标记。我会从 faces-config.xml 中删除该条目。This should work. Perhaps you already have a
messages*.properties
file in the classpath root of your main webapp. This one has then precedence in classloading. You need to put it in a more specific package. Put the JAR's one in for example thecom/portlet/common
folder so that it becomes member of thecom.portlet.common
package. This way it'll be available by:Unrelated to the concrete problem, the
<message-bundle>
entry infaces-config.xml
has a completely different purpose. It's supposed to override JSF default validation/conversion messages which are returned by JSF default validators/converters. It is not intended to provide localized content. There you use the<resource-bundle>
entry or<xxx:loadBundle>
tag for. I'd remove that entry from thefaces-config.xml
.