由组件生成的客户机id之间的差异。clientId和p:组件()

时间:2022-05-15 20:03:08

I am trying to retrieve the client id of a h:panelGroup that is within a p:dataList.

我正在尝试检索一个h的客户端id:panelGroup,它位于一个p:dataList中。

I tried 2 approaches:

我试着2个方法:

1.Using component.clientId e.g:

1。使用组件。clientId例句:

<h:panelGroup id="listItem">
    <h:outputText value="#{component.clientId}" />
</h:panelGroup>

2.Using p:component() e.g:

2。使用p:组件()例句:

<h:panelGroup id="listItem">
    <h:outputText value="#{p:component('listItem')}" />
</h:panelGroup>

Please note that this panel group is within a datalist. Now, the client ids generates in both the cases is different. (1) does not have the value 'listItem' appended to the client id while (2) has the value 'listItem' in the generated clientId.

请注意,这个面板组是在一个datalist中。现在,客户端id在两种情况下都会产生不同的结果。(1)没有将值'listItem'附加到客户端id,而(2)在生成的clientId中具有值'listItem'。

Also, the client id generated using (1) is different from that on the generated html component.

另外,使用(1)生成的客户机id与生成的html组件的id不同。

Could anyone shed some light on this issue as to why is this so ?

谁能解释一下为什么会这样?

2 个解决方案

#1


4  

The implicit EL object #{component} refers to the current component, which is in the case of

隐式EL对象#{component}是指当前组件,也就是当前组件。

<h:outputText value="#{component.clientId}" />

the <h:outputText> itself!

< h:outputText >本身!

If you intend to print the client ID of another component, then you need to bind the component instance to an unique variable in the view by binding, so that you can reference it anywhere else in the same view.

如果您打算打印另一个组件的客户端ID,那么您需要将组件实例绑定到视图中的一个唯一的变量,这样您就可以在相同视图的任何其他地方引用它。

<h:panelGroup id="listItem" binding="#{listItem}">
    <h:outputText value="#{listItem.clientId}" />
</h:panelGroup>

See also:

#2


1  

There are two premises in my answer:

在我的回答中有两个前提:

  1. UIPanel class doesn't implement NamingContainer interface, thus, id of <h:panelGroup> won't end up in client id of its children;
  2. UIPanel类没有实现NamingContainer接口,因此, 的id不会以其子的客户端id结束;
  3. #{component} resolves to the current component in which this variable is used.
  4. #{组件}解析到使用此变量的当前组件。

In this light in the first snippet you're outputting client id of an <h:outputText>, that is the id of its naming container plus separator plus its autogenerated id (note that you mispaced : that should be . in your #{component:clientId}), and in the second snippet you use EL function p:component of PrimeFaces that searches the whole component tree for the component with id as specified in its parameter and returns client id of the found component.

在第一个代码段中,您输出的是一个 的客户端id,这是它的命名容器加分隔符的id加上它的自动生成的id(注意,您的间隔是:应该是这样的)。在您的#{组件:clientId}中,在第二个代码段中,您使用EL函数p: PrimeFaces的组件,该组件通过在其参数中指定的id搜索整个组件树,并返回所找到组件的客户端id。

So, you look for different components: <h:outputText> in the first case and <h:panelGroup> in the second case, and that explains the difference in results.

因此,您需要寻找不同的组件: 在第一种情况下, 在第二种情况下,这解释了结果的差异。

Under current setup the following expressions will yield identical results:

在当前设置下,下列表达式将产生相同的结果:

  • #{component.parent.clientId} and
  • # { component.parent。clientId },
  • #{p:component('listItem')}.
  • # { p:组件(“列”)}。

#1


4  

The implicit EL object #{component} refers to the current component, which is in the case of

隐式EL对象#{component}是指当前组件,也就是当前组件。

<h:outputText value="#{component.clientId}" />

the <h:outputText> itself!

< h:outputText >本身!

If you intend to print the client ID of another component, then you need to bind the component instance to an unique variable in the view by binding, so that you can reference it anywhere else in the same view.

如果您打算打印另一个组件的客户端ID,那么您需要将组件实例绑定到视图中的一个唯一的变量,这样您就可以在相同视图的任何其他地方引用它。

<h:panelGroup id="listItem" binding="#{listItem}">
    <h:outputText value="#{listItem.clientId}" />
</h:panelGroup>

See also:

#2


1  

There are two premises in my answer:

在我的回答中有两个前提:

  1. UIPanel class doesn't implement NamingContainer interface, thus, id of <h:panelGroup> won't end up in client id of its children;
  2. UIPanel类没有实现NamingContainer接口,因此, 的id不会以其子的客户端id结束;
  3. #{component} resolves to the current component in which this variable is used.
  4. #{组件}解析到使用此变量的当前组件。

In this light in the first snippet you're outputting client id of an <h:outputText>, that is the id of its naming container plus separator plus its autogenerated id (note that you mispaced : that should be . in your #{component:clientId}), and in the second snippet you use EL function p:component of PrimeFaces that searches the whole component tree for the component with id as specified in its parameter and returns client id of the found component.

在第一个代码段中,您输出的是一个 的客户端id,这是它的命名容器加分隔符的id加上它的自动生成的id(注意,您的间隔是:应该是这样的)。在您的#{组件:clientId}中,在第二个代码段中,您使用EL函数p: PrimeFaces的组件,该组件通过在其参数中指定的id搜索整个组件树,并返回所找到组件的客户端id。

So, you look for different components: <h:outputText> in the first case and <h:panelGroup> in the second case, and that explains the difference in results.

因此,您需要寻找不同的组件: 在第一种情况下, 在第二种情况下,这解释了结果的差异。

Under current setup the following expressions will yield identical results:

在当前设置下,下列表达式将产生相同的结果:

  • #{component.parent.clientId} and
  • # { component.parent。clientId },
  • #{p:component('listItem')}.
  • # { p:组件(“列”)}。