使用JSF 2.0获取命名容器内的组件clientId的完整前缀 [英] Acquire full prefix for a component clientId inside naming containers with JSF 2.0

查看:121
本文介绍了使用JSF 2.0获取命名容器内的组件clientId的完整前缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在JSF中通过AJAX更新组件:

I am updating a component via AJAX in JSF:

<h:form>
    <h:outputLink>Click me
        <f:ajax event="click" render=":messages" />
    </h:outputLink>
</h:form>

<h:messages id="messages" globalOnly="true" />

作为< h:messages /> 驻留在之外< h:form /> 我必须在ID前面添加冒号()。这是有效的。

As the <h:messages /> resides outside <h:form /> I have to prefix the ID with colon (:). This works.

但是,如果我将相同的代码放入组件并将该组件包含到我的页面中,则代码将失败。原因是:messages 声明是指组件层次结构的根,而< h:messages /> 我想要更新的组件实际上位于我的自定义组件下,该组件位于页面下(因此位置的格式为:myComponent:messages

However, if I put this same code into a component and include that component into my page, the code fails. The reason is that :messages declaration refers to the root of the component hierarchy, while the <h:messages /> component that I want to update actually resides under my custom component, which resides under the page (so the location is of style :myComponent:messages.

在我的组件中,我如何获得< h:messages /> 组件的正确前缀?我知道我可以手动为我的组件分配ID,并使用它来为引用添加前缀(如:#{cc.attrs.id}:messages )。但是,我不知道此组件所在的组件层次结构级别,因此所需的前缀可能类似于:foo:bar:x:y:messages

Inside my component, how can I achieve the correct prefix to the <h:messages /> component? I know that I can manually assign ID to my component, and use that to prefix the reference (like :#{cc.attrs.id}:messages). However, I don't know at which level of component hierarchy this component lies, so the required prefix might even be something like :foo:bar:x:y:messages.

推荐答案

好像你可以通过表达式语言(EL)隐式对象( cc )访问当前前缀组件):

Seems like you can access the current prefix via Expression Language (EL) implicit objects (cc and component):


  • cc.clientId 返回当前复合组件的前缀

  • component.clientId 返回任何当前组件的前缀。

  • cc.clientId returns current composite component's prefix
  • component.clientId returns prefix for any current component.

例如,在页面中,通过

<myComponent id="foo">

在这个组件中,可以像这样获取客户端ID:

Inside this component, one can fetch the client IDs like this:

<h:outputText id="bar">
   <p>ClientId for the composite component: #{cc.clientId}</p>
   <p>ClientId for current any component: #{component.clientId}</p>
</h:outputText>

以下内容应打印为:

ClientId for the composite component: foo
ClientId for current any component: foo:bar

我从博客文章 JSF:使用组件标识符(id / clientId)。它声明这是JSF 2.0的新功能。在此之前,必须以编程方式从辅助bean中获取ID。

I got the pointer from blog post JSF: working with component identifiers (id/clientId). It states that this is a new feature for JSF 2.0. Before that one had to fetch the ID programmatically from a backing bean.

这篇关于使用JSF 2.0获取命名容器内的组件clientId的完整前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆