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

查看:21
本文介绍了使用 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" />

由于 位于 之外,我必须在 ID 前加上冒号 (:).这有效.

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

但是,如果我将相同的代码放入一个组件并将该组件包含在我的页面中,代码将失败.原因是 :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) 隐式对象(cccomponent>):

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

  • cc.clientId 返回当前composite 组件的前缀
  • component.clientId 返回任何当前组件的前缀.
  • cc.clientId returns current composite component's prefix
  • component.clientId returns prefix for any current component.

比如在一个页面中,通过

For example, in a page, call some component via

<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: working带有组件标识符 (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天全站免登陆