Jsf 在同一命名容器中重用facelets组合时避免重复ID
我有一个Jsf 在同一命名容器中重用facelets组合时避免重复ID,jsf,jsf-2,facelets,composition,Jsf,Jsf 2,Facelets,Composition,我有一个,它包含一些带有显式ID的元素和一些ajax事件,这些事件引用这些ID进行部分处理/更新。我将这个xhtml片段封装在组合中,这样我就可以在几个不同的地方使用它,而无需复制代码。但是,当我在一个页面中多次使用该组合(使用)时,会出现重复的id异常。JSF似乎没有将每个组合包装在自己的命名容器中(就像那样) 有没有一种简单的方法将我的作品包装在它自己的命名容器中? 或者,每次我想在公共命名容器中重用xhtml片段时,我都必须使用复合组件吗?当您包含多次相同的ui:compositionid
,它包含一些带有显式ID的元素和一些ajax事件,这些事件引用这些ID进行部分处理/更新。我将这个xhtml片段封装在组合中,这样我就可以在几个不同的地方使用它,而无需复制代码。但是,当我在一个页面中多次使用该组合(使用
)时,会出现重复的id异常。JSF似乎没有将每个组合包装在自己的命名容器中(就像
那样)
有没有一种简单的方法将我的作品包装在它自己的命名容器中?
或者,每次我想在公共命名容器中重用xhtml片段时,我都必须使用复合组件吗?当您包含多次相同的
ui:composition
id重复时。解决方案是在ui:include
中指定特定的ui:param
假设包含mycomposition.xhtml,则可以执行类似的操作:
<ui:include src="mycomposition.xhtml">
<ui:param name="idPrefix" value="first"/>
</ui:include>
...
<ui:include src="mycomposition.xhtml">
<ui:param name="idPrefix" value="second"/>
</ui:include>
...
然后在mycomposition.xhtml中,您应该声明如下ID(例如h:outputText):
现在,您可以在页面的其余部分将id引用为
{idPrefix}\u text
根据
模板的用途,您有几个选项:
。它创建了另一个NamingContainer
上下文(如
,
,朋友都这样做):
并使用该ID作为合成中组件ID的前缀
<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>
借
...
...
这样,您可以按如下方式使用它:
<my:some id="top" />
...
<my:some id="bottom" />
...
中定义的组件将分别在其ID中获得top:
和bottom:
前缀(再次注意,复合组件的ID
属性是可选的,否则JSF将自动生成一个)
f:subView
中似乎有帮助,如中所述。再次感谢@BalusC。我认为您必须能够执行类似选项的操作-很高兴知道。@BalusC,如果我使用这个f:subview,我可以继续以相同的方式使用java脚本吗?或者javascripts(使用页面的ID)需要相应地更改?
<my:some id="top" />
...
<my:some id="bottom" />
<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>
<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>
<my:some id="top" />
...
<my:some id="bottom" />