Jsf How to create a Naming Container

岁酱吖の 提交于 2020-01-09 18:23:50

问题


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.


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/3449370/jsf-how-to-create-a-naming-container

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!