EL 中的#{component} 到底是什么? [英] What exactly is #{component} in EL?

查看:27
本文介绍了EL 中的#{component} 到底是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 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}" />

另见:

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