When to prefix form id with colon

耗尽温柔 提交于 2020-01-01 11:56:48

问题


I have two datatables in two forms, forma and formg. Inside each form there is a p:dataTable, groupsa and groupsg. In each datatable there is a custom column that displays an image(h:graphicImage) called fava and favg.

When the image is clicked, the images from the other datatable will be updated.

<p:ajax event="click" listener="#{agent.toogleFavorite}"
update="fava, :formg:groupsg:favg" />

Without the colon I get an exception:

javax.faces.FacesException: Cannot find component with identifier "forma:agentsa:fava" referenced from "groupsg:0:favg".

What is the difference between formg:groupsg:favg and :formg:groupsg:favg?

I am using JSF2.0 and PrimeFaces 3.4.


回答1:


The : prefix will make it an absolute client ID and thus it will be searched relative to the UIViewRoot instead of the closest parent NamingContainer. You should (must) use it when you want to refer a component which is not inside the same closest parent NamingContainer. The <h:form> and <h:dataTable> (and <p:dataTable>) are NamingContainer components.

See also How to find out client ID of component for ajax update/render? Cannot find component with expression "foo" referenced from "bar" for a detailed explaination.




回答2:


Use the colon at the beginning when you want to search for the ID from the root of the page. It is necessary when the component that is calling for the update is not inside the same NamingContainer of the component that is being updated.

Don't use it when you want to search relatively to the same NamingContainer. You can do that when both components (the one calling the update and the one being updated) are inside the same NamingContainer.

By the way, "NamingContainers" are all the components that prepend their ID to their child components, like <h:form>, <p:dataTable>, <p:accordionPanel>, etc. Just look at the generated HTML to see if the parent is prepending its ID to its childs.

  • NamingContainer Javadoc.
  • The algorithm that JSF uses for finding components by their ID.
  • BalusC explains it in full detail here.


来源:https://stackoverflow.com/questions/12367597/when-to-prefix-form-id-with-colon

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