Jsf 如何创建命名容器
我的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这就是我使用 JSF1.2 和 Facelets 的方法:
我发现
和
实际上都不是命名容器,因此,在同一表单中多次使用同一组件将会失败,并出现重复 ID 异常。这似乎是一个糟糕的设计,因为组件的重点是可重用性。为了解决这个问题,我在每个组件中包含一个
并将其上的 id 设置为我的组件标记的参数:myComponent.xhtml:
并且在其他页面上使用它是简单(正确设置 taglib.xml 和 web.xml 后):
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:
and the using it on other pages is simple (after setting up taglib.xml and web.xml correctly):
我建议退后一步调查为什么会出现重复的 ID 问题。一旦确定了根本原因,只需以“通常”的方式修复它,而不是创建自己的
UINamingContainer
组件。重复 ID 错误有多种可能的原因,这应该可以帮助您进一步确定问题:
UIComponent
中相同UINamingContainer
组件。UIComponent
属性。f:subview
在 parent 页面而不是 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:
UIComponent
s inside the sameUINamingContainer
component.UIComponent
property of the same bean.f:subview
is been declared in the parent page instead of the include page.UINamingContainer
component.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.