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)}"
函数在EL中使用.
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对象).通常,您只能在组件的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.
例如如果是<h:inputText>
,它将引用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中如何工作?何时以及如何使用?
- Difference between client id generated by component.clientId and p:component()
- JSF component binding without bean property
- How does the 'binding' attribute work in JSF? When and how should it be used?
See also:
这篇关于EL中的#{component}到底是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!