EL 中的#{component} 到底是什么? [英] What exactly is #{component} in EL?
问题描述
根据 https://code.google.com/p/primefaces/issues/detail?id=4720, ComponentUtils.resolveWidgetVar(String expression, UIComponent component)
函数自 2013 年起在 Primefaces 中可用.它可以通过 "#{p:widgetVarFromContext(searchExpression, component)}"
函数.
According to https://code.google.com/p/primefaces/issues/detail?id=4720, The ComponentUtils.resolveWidgetVar(String expression, UIComponent component)
function is available in Primefaces since 2013. It can be used in EL by the "#{p:widgetVarFromContext(searchExpression, component)}"
function.
如果多个组件在不同的 NamingContainer
中具有相同的 id,但仍然存在于同一个视图中,这很有用.在这种情况下,#{p:widgetVar(searchExpression)}
函数只返回找到的最后一个.
This is useful in case of several components have the same id in different NamingContainer
, but are still present in the same view. In this case,
the #{p:widgetVar(searchExpression)}
function only returns the last one found.
但是我不明白如何引用必须作为 EL 的第二个参数传递的 UIComponent
.上面提到的错误报告建议我们可以使用 #{component}
来引用它.谁能给我举个例子?
I don't understand however how to reference the UIComponent
that must be passed as the second argument from EL. The above mentioned bug report suggests we can refer to it using #{component}
. Can anyone provide me with an example?
推荐答案
#{component}
是一个隐式 EL 变量,引用了 EL 中的当前 UIComponent
范围(另见隐式EL对象).您通常只能在组件的 HTML 属性或模板文本子项中引用它.
The #{component}
is an implicit EL variable referring the current UIComponent
in EL scope (see also implicit EL objects). You can usually only refer it in component's HTML attribute or in template text children.
例如在
的情况下,它将引用 UIInput
类,其中有一个 isValid()
方法.
E.g. in case of <h:inputText>
it will reference an instance of UIInput
class which has among others an isValid()
method.
<h:inputText id="foo" required="true"
style="background: #{component.valid ? '' : 'pink'}"
onclick="alert('Client ID of this component is #{component.clientId}');" />
您还可以使用 binding
属性让 JSF 在视图构建期间将引用放入 Facelet 范围内的组件实例.这样,在视图渲染期间,组件引用将在 Facelet 中的任何位置可用.
You can also use binding
attribute to let JSF during view build time put a reference to a component instance in the Facelet scope. This way the component reference will be available anywhere in the Facelet during view render time.
<script>alert('Client ID of foo component is #{foo.clientId}');</script>
<h:inputText binding="#{foo}" />
另见:
- 组件生成的客户端 ID 之间的差异.clientId 和 p:component()
- 没有 bean 属性的 JSF 组件绑定
- 绑定"属性在 JSF 中是如何工作的?何时以及如何使用?
这篇关于EL 中的#{component} 到底是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!