java 使用 JSF 2.0 在命名容器中获取组件 clientId 的完整前缀

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3501022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 02:07:56  来源:igfitidea点击:

Acquire full prefix for a component clientId inside naming containers with JSF 2.0

javajsfreferencecomponentsjsf-2

提问by Tuukka Mustonen

I am updating a component via AJAX in JSF:

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

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

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

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

由于<h:messages />驻留在外面,<h:form />我必须在 ID 前加上冒号 ( :)。这有效。

However, if I put this same code into a component and include that component into my page, the code fails. The reason is that :messagesdeclaration 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.

但是,如果我将相同的代码放入一个组件并将该组件包含在我的页面中,则代码将失败。原因是:messages声明引用了组件层次结构的根,而<h:messages />我要更新的组件实际上驻留在我的自定义组件下,该组件驻留在页面下(因此位置为 style :myComponent: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.

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

回答by Tuukka Mustonen

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

似乎您可以通过表达式语言 (EL) 隐式对象 (cccomponent)访问当前前缀:

  • cc.clientIdreturns current compositecomponent's prefix
  • component.clientIdreturns prefix for any current component.
  • cc.clientId返回当前复合组件的前缀
  • component.clientId返回任何当前组件的前缀。

For example, in a page, call some component via

例如,在一个页面中,通过调用某个组件

<myComponent id="foo">

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

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

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

The following should print out as:

以下应打印为:

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

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: working with component identifiers (id/clientId) 中得到了指针。它声明这是 JSF 2.0 的一个新特性。在此之前,必须以编程方式从支持 bean 中获取 ID。

回答by Ziletka

@Tuukka Mustonen, thank you for your answer.

@Tuukka Mustonen,感谢您的回答。

And if we need access some another id from the same "place" we could use:

如果我们需要从同一个“地方”访问另一个 id,我们可以使用:

<h:outputScript name="js/my-script.js"/>
<h:form id="myForm">
    <h:inputText id="aaa"
        onkeyup="myJSFunction(this.id)"
        value="..."
    />
    <h:inputText id="bbb"
        onkeyup="myJSFunction(aaa.id)"
        value="..."
    />
    <h:inputText id="ccc"
        onkeyup="myJSFunction('#component.parent.clientId.concat(':ccc')}')"
        value="..."
    />
    <h:inputText id="ddd"
        onkeyup="myJSFunction('#component.parent.clientId.concat(':aaa')}')"
        value="..."
    />
</h:form>

JavaScript function:

JavaScript 函数:

function myJSFunction(message) {
    window.alert(message)
}

Output on dialog window:

对话窗口输出:

1) myForm:aaa

1)我的表格:aaa

2) null

2)

3) myForm:ccc

3) myForm:ccc

4) myForm:aaa

4)我的表格:aaa

Note: so the 1st and 3th have the same output.

注意:所以第 1 个和第 3 个具有相同的输出。