在同一命名容器中重复使用facelets组合时,避免重复的id [英] Avoiding duplicate ids when reusing facelets compositions in the same naming container

查看:118
本文介绍了在同一命名容器中重复使用facelets组合时,避免重复的id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个<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:

  1. 使用<f:subview>.它会创建另一个NamingContainer上下文(例如<h:form><h:dataTable>和朋友都可以这样做):

  1. Use <f:subview>. It creates another NamingContainer 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).

    另请参见:

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆