java—在JSF2.0中检索其他组件的客户机id

xmjla07d  于 2021-07-09  发布在  Java
关注(0)|答案(3)|浏览(419)

jsf2.0是否有用于查找另一个组件的客户机id的内置方法?有上千个与客户机id相关的问题,有很多黑客方法可以做到这一点,但是我想知道jsf2.0是否带来了一个我不知道的更简单的方法。 #{component.clientId} 计算为给定组件自己的客户端id,但我要引用另一个组件的id。
这篇博文提到 component.clientId ,它还说 #{someComponent.clientId} 行得通,但据我所知不行。我相信他是在JSF2.0的任何参考实现出现之前写的,所以他只是按照jsr去做,也许功能改变了。我不确定。
我知道primefaces和richfaces都有自己的函数来返回客户机id,但我只是想知道是否有一个内置的jsf2.0方法来实现这一点。以下是一些示例:
这用于返回outputtext的id。

`<h:outputText value="My client ID : #{component.clientId}" />`

根据上面的博客文章,这应该是可行的,但事实并非如此。我只是没有输出。

`<h:button id="sampleButton" value="Sample" />`

`<h:outputText value="sampleButton's client ID : #{sampleButton.clientId}" />`

这适用于primefaces:

`<h:outputText value="PrimeFaces : sampleButton's client ID : #{p:component('sampleButton')}" />`

在richfaces工作:

`<h:outputText value="RichFaces : sampleButton's client ID : #{rich:clientId('sampleButton')}" />`

另外,如果有可能的话,我正在寻找一个解决方案,如果我改变了规则,这个解决方案是不会被打破的 javax.faces.SEPARATOR_CHAR 值或是否在引用的组件外部添加/移除容器。我花了大量时间跟踪硬编码id路径引起的问题。

gt0wga4j

gt0wga4j1#

这对我有用。我很想知道是否可以写这样的回应,虽然。
客户端.html

<h:outputText value="#{UIHelper.clientId('look-up-address-panel-id')}" />

uihelper.java文件

@ManagedBean(name = "UIHelper", eager = true)
@ApplicationScoped
public class UIHelper
{

public String clientId(final String id)
{
  FacesContext context = FacesContext.getCurrentInstance();
  UIViewRoot root = context.getViewRoot();
  final UIComponent[] found = new UIComponent[1];
  root.visitTree(new FullVisitContext(context), new VisitCallback()
  {
    @Override
    public VisitResult visit(VisitContext context, UIComponent component)
    {
      if (component.getId().equals(id))
      {
        found[0] = component;
        return VisitResult.COMPLETE;
      }
      return VisitResult.ACCEPT;
    }
  });
  return found[0] == null ? "" : "#" + found[0].getClientId().replace(":", "\\\\:");
}

}
v9tzhpje

v9tzhpje2#

因为这是我在谷歌搜索的第一个结果,我想知道为什么我得到了一个
javax.el.propertynotfoundexception(未找到属性“itemid”[…])
在尝试接受的解决方案时,我想与大家分享我的jsf 1.2解决方案:
这个 UIComponent 的方法 getClientId 需要一个 FacesContext 参数(请参阅uicomponent文档)。因此,向backing bean添加一个绑定,并添加另一个返回clientid的方法:
xhtml格式:

<h:button id="sampleButton" binding="#{backingBean.sampleButton}" value="Sample" />
<h:outputText value="sampleButton's client ID : #{backingBean.sampleButtonClientId}" />

豆子:

private UIComponent sampleButton;

public UIComponent getSampleButton() {
    return sampleButton;
}

public void setSampleButton(final UIComponent sampleButton) {
    this.sampleButton = sampleButton;
}

public String getSampleButtonClientId() {
    final FacesContext context = FacesContext.getCurrentInstance();
    return sampleButton.getClientId(context);
}

请注意,要将组件绑定到的bean应该是请求范围的,否则可能会导致 java.lang.IllegalStateException (duplicate Id for a component) (与本主题比较)。

7lrncoxx

7lrncoxx3#

您需要在视图范围中为组件指定一个变量名 binding 属性。

<h:button id="sampleButton" binding="#{sampleButton}" value="Sample" />
<h:outputText value="sampleButton's client ID : #{sampleButton.clientId}" />

相关问题