如何使用 JSF 查看 Jar 中的 message.properties?

发布于 2024-12-01 05:31:56 字数 2870 浏览 1 评论 0原文

使用: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:

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 技术交流群。

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

发布评论

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

评论(1

半枫 2024-12-08 05:31:56

这应该有效。也许您的主 Web 应用程序的类路径根目录中已经有一个 messages*.properties 文件。这个在类加载中具有优先权。您需要将其放入更具体的包中。将 JAR 放入例如 com/portlet/common 文件夹中,以便它成为 com.portlet.common 包的成员。这样,它就可以通过以下方式获得:

<a4j:loadBundle basename="com.portlet.common.messages" var="msgs"/>

与具体问题无关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 the com/portlet/common folder so that it becomes member of the com.portlet.common package. This way it'll be available by:

<a4j:loadBundle basename="com.portlet.common.messages" var="msgs"/>

Unrelated to the concrete problem, the <message-bundle> entry in faces-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 the faces-config.xml.

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