我正在研究一个现有的 JSF 组件,该组件的encodeEnd
方法以:
// popComponentFromEL(context);
JavadocUIComponent#popComponentFromEL(FacesContext context)
告诉我:
UIComponent
从属性映射中弹出当前组件,FacesContext
以便前一个UIComponent
(如果有)成为当前组件。
您何时以及为什么需要或想要它?
我发现同一个库中的其他组件都没有使用它。
我正在研究一个现有的 JSF 组件,该组件的encodeEnd
方法以:
// popComponentFromEL(context);
JavadocUIComponent#popComponentFromEL(FacesContext context)
告诉我:
UIComponent
从属性映射中弹出当前组件,FacesContext
以便前一个UIComponent
(如果有)成为当前组件。
您何时以及为什么需要或想要它?
我发现同一个库中的其他组件都没有使用它。
那是pushComponentToEL()
其 Javadoc 更详细地解释这一点的对应物。
pushComponentToEL
public final void pushComponentToEL(FacesContext context, UIComponent component)
将当前的 UIComponent this 推送到
FacesContext
属性映射,使用CURRENT_COMPONENT
保存先前UIComponent
关联的键以CURRENT_COMPONENT
供后续调用popComponentFromEL(javax.faces.context.FacesContext)
.This method and
popComponentFromEL()
form the basis for the contract that enables the EL Expression "#{component}
" to resolve to the "current" component that is being processed in the lifecycle. The requirements for whenpushComponentToEL()
andpopComponentFromEL()
must be called are specified as needed in the javadoc for this class. AfterpushComponentToEL()
returns, a call togetCurrentComponent(javax.faces.context.FacesContext)
must return thisUIComponent
instance untilpopComponentFromEL()
is called, after which point the previousUIComponent
instance will be returned fromgetCurrentComponent()
Basically, this approach
public void encodeXxx(FacesContext context) {
try {
pushComponentToEL(context, this);
// ...
}
finally {
popComponentFromEL(context);
}
}
allows you during the // ...
process to grab this
component using #{component}
in EL, or UIComponent#getCurrentComponent()
in a managed bean.
One of well known examples is this construct:
<h:inputText ... styleClass="#{component.valid ? 'valid' : 'error'}" />
where #{component.valid}
basically refers UIInput#isValid()
.