在同一命名容器中重复使用facelets组合时,避免重复的id [英] Avoiding duplicate ids when reusing facelets compositions in the same naming container
问题描述
我有一个<ui:composition>
,其中包含一些带有显式ID的元素和一些ajax事件,这些事件引用这些ID进行部分处理/更新.我只是将xhtml的这个片段封装在合成中,所以我可以在几个不同的地方使用它,而不必重复代码.但是,当我在页面中多次使用合成(<ui:include>
)时,会出现重复的id异常.似乎JSF并未将每个合成都包装在其自己的命名容器中(就像<ui:component>
一样).
I have a <ui:composition>
that contains a few elements with explicit ids and some ajax events which reference these ids for partial processing/updating. I encapsulated this fragment of xhtml inside the composition simply so I could use it in a few different places without having to duplicate the code. However, when I use the composition (with <ui:include>
) more than once inside a page, I get duplicate id exceptions. It seems JSF is not wrapping each composition inside its own naming container (like <ui:component>
does).
有没有一种简单的方法可以将我的作品包装在其自己的命名容器中? 还是我每次想在通用命名容器内重用xhtml片段时都必须使用复合组件?
Is there a simple way to wrap my composition inside its own naming container? Or do I have to use a composite component every time I want to reuse xhtml fragments inside a common naming container?
推荐答案
根据<ui:include>
模板的目的,您有几种选择:
Depending on the purpose of the <ui:include>
template, you've several options:
-
使用
<f:subview>
.它会创建另一个NamingContainer
上下文(例如<h:form>
,<h:dataTable>
和朋友都可以这样做):
Use
<f:subview>
. It creates anotherNamingContainer
context (like as<h:form>
,<h:dataTable>
, and friends all do):
<f:subview id="top">
<ui:include src="/WEB-INF/includes/some.xhtml" />
</f:subview>
...
<f:subview id="bottom">
<ui:include src="/WEB-INF/includes/some.xhtml" />
</f:subview>
在some.xhtml
中定义的组件最终将在其ID中分别获得top:
和bottom:
前缀.
The components definied in some.xhtml
will end up getting respectively top:
and bottom:
prefix in their ID.
将其转换为标记文件,该属性需要id
属性.
Turn it into a tagfile which requires an id
attribute.
<my:some id="top" />
...
<my:some id="bottom" />
并使用该ID在合成中添加组件ID的前缀.
And use that ID to prefix the ID of the components in the composition.
<ui:composition
xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
>
...
<h:someComponent id="#{id}_some" />
<h:otherComponent id="#{id}_other" />
...
<ui:composition>
将其转换为复合组件.复合组件本来就已经是NamingContainer
,因此它们的id
属性是可选的.基本上,替换
Turn it into a composite component. Composite components are inherently already a NamingContainer
, so their id
attribute is optional. Basically, replace
<ui:composition
xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
>
...
<ui:composition>
作者
<ui:component
xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:cc="http://java.sun.com/jsf/composite"
>
<cc:interface>
<!-- This is optional. -->
</cc:interface>
<cc:implementation>
...
<h:someComponent id="some" />
<h:otherComponent id="other" />
...
</cc:implementation>
<ui:component>
这样,您可以按以下方式使用它:
This way you can use it as follows:
<my:some id="top" />
...
<my:some id="bottom" />
在<cc:implementation>
中定义的组件最终将在其ID中分别获得top:
和bottom:
前缀(再次注意,复合组件的id
属性是可选的,否则JSF将自动生成一个).
The components definied in <cc:implementation>
will end up getting respectively top:
and bottom:
prefix in their ID (note again, the composite component's id
attribute is optional, JSF will otherwise autogenerate one).
另请参见:
- 何时使用< ui:include> ;、标记文件,复合组件和/或自定义组件?
- When to use <ui:include>, tag files, composite components and/or custom components?
See also:
这篇关于在同一命名容器中重复使用facelets组合时,避免重复的id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!