简体   繁体   English

Jsf如何创建命名容器

[英]Jsf How to create a Naming Container

I have a problem with duplicated ids in my JSF app. 我的JSF应用程序中存在重复ID的问题。 I've read in this post that one of the possible solutions is to use Naming Container. 在这篇文章中读过,其中一个可能的解决方案是使用命名容器。 Can you give me some example how to use the Naming Container to avoid duplicated ids problem? 你能给我一些例子来说明如何使用命名容器来避免重复的id问题吗? I use Facelets. 我使用Facelets。

This is what worked for me using JSF1.2 and facelets: 这对我来说使用JSF1.2和facelets是有用的:

I discovered that neither <ui:composition> nor <ui:component> is actually a naming container, so using the same component more than once in the same form would fail with a duplicate ID exception. 我发现<ui:composition><ui:component>实际上都不是命名容器,因此在同一表单中多次使用同一个组件会因重复的ID异常而失败。 This seems like a bad design, as the whole point of components is re-usability. 这似乎是一个糟糕的设计,因为组件的重点是可重用性。 To get around this problem I include a <f:subview> within each component and set the id on it as a parameter of my component tag: 为了解决这个问题,我在每个组件中包含一个<f:subview> ,并将其上的id设置为我的组件标记的参数:

myComponent.xhtml: myComponent.xhtml:

<ui:component>      
    <f:subview id="#{id}">
        ....component code
    </f:subview>
</ui:component>

and the using it on other pages is simple (after setting up taglib.xml and web.xml correctly): 并且在其他页面上使用它很简单(在正确设置taglib.xml和web.xml之后):

<myTagLib:myComponent id="myCompA" />

I suggest to take a step back and investigate why the duplicate ID problem occurs. 我建议退后一步,研究为什么会出现重复的ID问题。 Once you nailed the root cause down, then just fix it the "usual" way rather than creating your own UINamingContainer component. 一旦你确定了根本原因,然后只需修复它“通常”的方式,而不是创建自己的UINamingContainer组件。

There are several possible causes for duplicate ID errors which should help you further nailing it down: 重复ID错误有几种可能的原因,这有助于您进一步确定错误:

  • The same ID is used on different UIComponent s inside the same UINamingContainer component. 同一 UINamingContainer组件内的不同 UIComponent上使用相同的 ID。
  • Physically different components are bound to the same UIComponent property of the same bean. 物理上不同的组件绑定到 一个 bean的相同 UIComponent属性。
  • JSP only: the f:subview is been declared in the parent page instead of the include page. 仅限JSP: f:subview已在页面而不是包含页面中声明。
  • The same include page is included multiple times inside the same UINamingContainer component. 同样包括页面包含相同的多次 UINamingContainer组件。
  • A component is been dynamically built (eg new UIComponent() ) without having an ID assigned. 组件已动态构建(例如, new UIComponent() ), 但未分配ID。

Here, UINamingContainer is under each the <h:form> , <h:dataTable> and <f:subview> . 这里, UINamingContainer位于<h:form><h:dataTable><f:subview>之下。

If the above suggestions doesn't help, then update your question to include the smallest possible code snippet (thus, without all irrelevant code/clutter like unrelated components, libraries, HTML/CSS/JS/etc) which reproduces the exact same problem by just copy'n'paste'n'running it without any changes. 如果以上建议没有帮助,那么更新您的问题以包含尽可能小的代码片段(因此, 没有所有不相关的代码/混乱,如不相关的组件,库,HTML / CSS / JS /等),它们通过以下方式重现完全相同的问题:只是复制'n'paste'n'运行它没有任何变化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM