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

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

问题描述

我有一个 <ui:composition>,其中包含一些具有显式 ID 的元素和一些引用这些 ID 以进行部分处理/更新的 ajax 事件.我将这个 xhtml 片段简单地封装在组合中,这样我就可以在几个不同的地方使用它而不必复制代码.但是,当我在页面内多次使用组合(带有 )时,我会收到重复的 id 异常.似乎 JSF 并没有将每个组合包装在它自己的命名容器中(就像 那样).

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?

推荐答案

根据 模板的用途,您有多种选择:

Depending on the purpose of the <ui:include> template, you've several options:

  1. 使用.它创建了另一个 NamingContainer 上下文(例如 ,朋友们都这样做):

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

    中定义的组件最终会在它们的 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天全站免登陆