包含相同的 XHTML JSF 2.0 文件时如何避免命名空间冲突

发布于 2025-01-04 10:55:50 字数 1313 浏览 0 评论 0原文

在我的 JSF 2.0 项目中,当我在单个父 XHTML 文件中包含两个相同的 XHTML 文件时,会发生名称空间冲突。我的目标是在单个页面上创建包含相同控件的仪表板,每个控件代表一个单独的上下文。

例如,在下面的名为 parent.xhtml (假设)的 JSF 文件中,我包含两个“仪表板”facelet:

<ui:define name="body">
    <p:panel id="ONE"  >
        <ui:include src="raceboardunit.xhtml"/>
    </p:panel>

    <p:panel id="TWO" >
        <ui:include src="raceboardunit.xhtml"/>
    </p:panel>
</ui:define>

简单来说,假设子 Facelet“raceboardunit.xhtml”如下

<ui:composition 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:p="http://primefaces.prime.com.tr/ui">

    <p:panel header="Dashboard" >
        <h:panelGrid columns="2">
            <h:outputLabel value="Event" for="idEvent}" />
            <h:outputLabel value="#raceBoardController.name}" id="idEvent" />
        </h:panelGrid>
    </p:panel>
</ui:composition>

:问题是涉及输出标签 id="idEvent" 的冲突,因为在 parent.xhtml 文件中,子 Facelet 的两个包含项具有相同的命名组件。

错误报告为:组件 ID idEvent 已在视图中找到。

错误的原因很明显,但是在视图中包含可变数量的相同组件的最佳方法是什么?最大化代码重用的 JSF 2.0 应用程序?

In my JSF 2.0 project, I get a name-space collision when I include two identical XHTML files in a single parent XHTML file. My goal is to create a dashboard of identical controls on a single page, with each control representing a separate context.

For example, in the following JSF file, called parent.xhtml (say), I include two "dashboard" facelets:

<ui:define name="body">
    <p:panel id="ONE"  >
        <ui:include src="raceboardunit.xhtml"/>
    </p:panel>

    <p:panel id="TWO" >
        <ui:include src="raceboardunit.xhtml"/>
    </p:panel>
</ui:define>

To be simple, say the child facelet "raceboardunit.xhtml" is the following:

<ui:composition 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:p="http://primefaces.prime.com.tr/ui">

    <p:panel header="Dashboard" >
        <h:panelGrid columns="2">
            <h:outputLabel value="Event" for="idEvent}" />
            <h:outputLabel value="#raceBoardController.name}" id="idEvent" />
        </h:panelGrid>
    </p:panel>
</ui:composition>

The problem is the collision involving the output label id="idEvent", since in the parent.xhtml file, the two inclusions of the child facelet have an identical named component.

The error is reported as: Component ID idEvent has already been found in the view.

The reason for the error is obvious, but what is the best way to include a variable number of identical components in a JSF 2.0 application that maximises code reuse?

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

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

发布评论

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

评论(1

指尖上得阳光 2025-01-11 10:55:50

最简单但最丑陋的方法是将其包装在 基本上引入了一个新的 UINamingContainer 层。

<p:panel id="ONE">
    <f:subview id="board1">
        <ui:include src="raceboardunit.xhtml"/>
    </f:subview>
</p:panel>
<p:panel id="TWO">
    <f:subview id="board2">
        <ui:include src="raceboardunit.xhtml"/>
    </f:subview>
</p:panel>

更好的方法是使其成为标签文件或复合组件。

这是一个标签文件的示例:

<p:panel id="ONE">
    <my:raceboardunit id="board1" />
</p:panel>
<p:panel id="TWO">
    <my:raceboardunit id="board2" />
</p:panel>

with in tag file

<h:outputLabel for="#{id}_idEvent" value="Event" />
<h:inputText id="#{id}_idEvent" value="#{raceBoardController.name}" />

(我认为您过于简化了问题中的示例,以至于没有任何意义;一个标签引用了另一个标签?所以我已经还修复了它,使其具有一定的意义。)

复合组件的用法与标记文件基本相同,但其本身隐式已经是一个 UINamingContainer,因此您不需要手动为复合组件内的 ID 添加前缀 执行。

<h:outputLabel for="idEvent" value="Event" />
<h:inputText id="idEvent" value="#{raceBoardController.name}" />

另请参阅:

Easiest but ugliest way is to wrap it in a <f:subview> which basically introduces a new UINamingContainer layer.

<p:panel id="ONE">
    <f:subview id="board1">
        <ui:include src="raceboardunit.xhtml"/>
    </f:subview>
</p:panel>
<p:panel id="TWO">
    <f:subview id="board2">
        <ui:include src="raceboardunit.xhtml"/>
    </f:subview>
</p:panel>

Better is to make it a tag file or a composite component instead.

Here's an example of how it can look like as a tag file:

<p:panel id="ONE">
    <my:raceboardunit id="board1" />
</p:panel>
<p:panel id="TWO">
    <my:raceboardunit id="board2" />
</p:panel>

with in tag file

<h:outputLabel for="#{id}_idEvent" value="Event" />
<h:inputText id="#{id}_idEvent" value="#{raceBoardController.name}" />

(I think that you oversimplified the example in the question too much that it made no sense; a label referring another label? So I've also fixed it so that it makes somewhat sense.)

A composite component has basically the same usage as a tag file, but is by itself implicitly already an UINamingContainer, so you wouldn't need to manually prefix the IDs inside the composite component implementation.

<h:outputLabel for="idEvent" value="Event" />
<h:inputText id="idEvent" value="#{raceBoardController.name}" />

See also:

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