获取命名容器中组件真实 ID 的更好方法

发布于 2024-12-18 07:45:51 字数 797 浏览 1 评论 0原文

我得到以下场景: 我有几个选项卡(TabView 是一个命名容器) 在其中一个我得到 ap:inputText 显示一个对话框(位于其他 xhtml 文件中),现在我希望对话框更新 p:inputText ,问题是 p:inputText 的 id 未知( JSF 添加了一些前缀)

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

我无法在

ATM 对话框中使用此 update="hoursReportFrm:comment4Dialog" 我查看了此站点的示例 JSF:使用组件 ID(id 与 clientId) (从 2009 年开始)

并添加了对 inputtext 的绑定,例如 binding="#{lookup.components.comment4Dialog}" 以及在 p:commandButton 中我将对话框更改为 update="#{lookup.clientIds.comment4Dialog}"

并且它工作得很好,但我正在寻找更好的方法,而不需要绑定我想要的每个组件稍后访问...

先谢谢您,

I got the following scenario:
I got several tabs (TabView is a naming container )
and in one of the I got a p: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 thisbinding="#{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,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月静好 2024-12-25 07:45:51

老实说,我认为绑定可能是最简单的途径,但是当我遇到这种情况时,我发现复合组件通常提供比绑定更好的解决方案。根据具体情况(再次强调,完全是个案),您可以使用复合组件来解决这个问题。这使您可以创造性地重用页面的某些部分,这样您的特定更新就不会花费大量工作,并且可以重用代码。

这方面的一个例子可能是:

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

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

这里可能是正在使用的组件:

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

通过将此视图分离为一段可重用的代码,您可能能够摆脱指定完整路径的负担,因为它现在更容易重用。然而,我认为这是一个绑定或复合组件的折腾,具体取决于具体情况。为了重用,创建一个新对象(组件)并重用它。如果您正在处理高度动态的环境:绑定它。

无论如何,我不是专家,但总的来说,上述内容使我摆脱了很多困境。

编辑:重新阅读时,您还应该确保选项卡视图没有延迟加载,并查看渲染以确保路径正确(检查 ID)。我遇到过问题(在旧版本的 Primefaces 中),有时 id 嵌套在 ap:outputPanel 中,或者在极少数情况下嵌套在子视图 id 中。通过指定完整路径 ':subview:form:component' 可能是一个非常简单的修复,但事实并非如此。

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 a p: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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文