Jsf 如何创建命名容器

发布于 2024-09-14 05:02:36 字数 192 浏览 10 评论 0原文

我的 JSF 应用程序中存在重复 id 的问题。我在这篇文章中读到,可能的解决方案之一是使用命名容器。您能给我一些如何使用命名容器来避免重复 ID 问题的示例吗?我使用 Facelets。

I have a problem with duplicated ids in my JSF app. 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? I use Facelets.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寄风 2024-09-21 05:02:36

这就是我使用 JSF1.2 和 Facelets 的方法:

我发现 实际上都不是命名容器,因此,在同一表单中多次使用同一组件将会失败,并出现重复 ID 异常。这似乎是一个糟糕的设计,因为组件的重点是可重用性。为了解决这个问题,我在每个组件中包含一个 并将其上的 id 设置为我的组件标记的参数:

myComponent.xhtml:

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

并且在其他页面上使用它是简单(正确设置 taglib.xml 和 web.xml 后):

<myTagLib:myComponent id="myCompA" />

This is what worked for me using JSF1.2 and 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. 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:

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):

<myTagLib:myComponent id="myCompA" />
番薯 2024-09-21 05:02:36

我建议退后一步调查为什么会出现重复的 ID 问题。一旦确定了根本原因,只需以“通常”的方式修复它,而不是创建自己的 UINamingContainer 组件。

重复 ID 错误有多种可能的原因,这应该可以帮助您进一步确定问题:

  • 相同 ID 用于不同 UIComponent相同 UINamingContainer 组件。
  • 物理上不同的组件绑定到相同 bean 的相同 UIComponent 属性。
  • 仅限 JSP:f:subviewparent 页面而不是 include 页面中声明。
  • 相同包含页面在相同 UINamingContainer 组件内多次包含。
  • 组件是动态构建的(例如new UIComponent()),没有分配ID。

在这里, UINamingContainer 位于 之下。

如果上述建议没有帮助,请更新您的问题以包含尽可能小的代码片段(因此,没有所有不相关的代码/混乱,例如不相关的组件、库、HTML/CSS/JS/等)只需复制“粘贴”然后运行它而不进行任何更改即可重现完全相同的问题。

I suggest to take a step back and investigate why the duplicate ID problem occurs. Once you nailed the root cause down, then just fix it the "usual" way rather than creating your own UINamingContainer component.

There are several possible causes for duplicate ID errors which should help you further nailing it down:

  • The same ID is used on different UIComponents inside the same UINamingContainer component.
  • Physically different components are bound to the same UIComponent property of the same bean.
  • JSP only: the f:subview is been declared in the parent page instead of the include page.
  • The same include page is included multiple times inside the same UINamingContainer component.
  • A component is been dynamically built (e.g. new UIComponent()) without having an ID assigned.

Here, UINamingContainer is under each the <h:form>, <h:dataTable> and <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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文