Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jsf 在同一命名容器中重用facelets组合时避免重复ID_Jsf_Jsf 2_Facelets_Composition - Fatal编程技术网

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" />