简体   繁体   中英

A better way to get the real ID of an component in a naming container

I got the following scenario: I got several tabs (TabView is a naming container ) and in one of the I got ap:inputText which shows a dialog(which is located in other xhtml file) , now I want the dialog to update the p:inputText , the thing is that the id of the p:inputText is unknow (JSF adds some prefix to it)

<h:form id="hoursReportFrm">
    <p:inputText id="comment4Dialog" value="#{hoursReportBean.aComment}" 
onfocus="dlg1.show();"/>

I can't use this update="hoursReportFrm:comment4Dialog" in the dialog

ATM i looked at the example of this site JSF: working with component IDs (id vs clientId) (its from 2009)

and added binding to to inputtext , like this binding="#{lookup.components.comment4Dialog}" and in the p:commandButton of the dialog I changed to update="#{lookup.clientIds.comment4Dialog}"

and It works just fine, but I'm looking for a better way , without the need to bind every component that I would like to access later...

Thanks ahead,

To be quite honest, I think the binding is probably the easiest route, however when I've been in that situation I've found composite components often offer a better solution than bindings. Depending on the situation (and again, its totally case by case) you can use a composite component to get around this problem. This allows you to reuse parts of a page creatively so that your specific updates don't take a lot of work and you can reuse the code.

An example of this might be:

//comp:myDialog
...
<composite:interface>
  <composite:attribute name="update" />
  <!-- Other attributes -->
</composite:interface>

<composite:implementation>
  ...
  <!-- Implementation -->
  <p:commandButton update="#{cc.attrs.update}"/>
  ...
</composite:implementation>

And here might be the component in use:

//for the sake of argument 'comp' as your library
<h:form id="someForm">
  <p:inputText value="#{bean.changeMe}" id="changeMe"/>
</h:form>

<h:form id="dialog">
  <!-- dialog here -->
  <comp:myDialog update="someForm:changeMe" />
</h:form>

By separating this view into a piece of reusable code you might be able to get around the burden of specifying the full path because it is now much easier to reuse. However, I think it is a toss up of a binding or the composite component depending on the specific case. For reuse, make a new object (component) and reuse it. If you're dealing with a highly dynamic environment: bind it.

I'm not an expert by any means, but generally speaking the above has gotten me out of a lot of tough situations.

EDIT: on a re-read you should also make sure that the tab-view isn't lazily loaded and take a look at the rendering to make sure the path is correct (inspect the ID). I've had problems (in older versions of Primefaces) where sometimes the id was nested inside ap:outputPanel or in rare cases the subview id. It might be a very simple fix by specifying the full path ':subview:form:component' though that shouldn't be the case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM