开发者

Jsf How to create a Naming Container

开发者 https://www.devze.com 2023-01-11 11:52 出处:网络
I have a problem with duplicated ids in my JSF app. I\'ve read in this post that one of the possible so开发者_JAVA百科lutions is to use Naming Container. Can you give me some example how to use the Na

I have a problem with duplicated ids in my JSF app. I've read in this post that one of the possible so开发者_JAVA百科lutions 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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消