包含相同的 XHTML JSF 2.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最简单但最丑陋的方法是将其包装在
基本上引入了一个新的UINamingContainer
层。更好的方法是使其成为标签文件或复合组件。
这是一个标签文件的示例:
with in tag file
(我认为您过于简化了问题中的示例,以至于没有任何意义;一个标签引用了另一个标签?所以我已经还修复了它,使其具有一定的意义。)
复合组件的用法与标记文件基本相同,但其本身隐式已经是一个
UINamingContainer
,因此您不需要手动为复合组件内的 ID 添加前缀 执行。另请参阅:
Easiest but ugliest way is to wrap it in a
<f:subview>
which basically introduces a newUINamingContainer
layer.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:
with in tag file
(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.See also: