我什么时候应该使用 UIComponent#popComponentFromEL(FacesContext context)?

When should I use UIComponent#popComponentFromEL(FacesContext context)?

我正在处理现有的 JSF 组件,其中 encodeEnd 方法以:

结尾
// popComponentFromEL(context);

UIComponent#popComponentFromEL(FacesContext context) 的 Javadoc 告诉我:

Pop the current UIComponent from the FacesContext attributes map so that the previous UIComponent, if any, becomes the current component.

您何时以及为何需要或想要它?

我发现同一库中的 none 个其他组件正在使用它。

这是 pushComponentToEL() 的对应物,其 Javadoc 对此进行了更详尽的解释。

pushComponentToEL

public final void pushComponentToEL(FacesContext context, UIComponent component)

Push the current UIComponent this to the FacesContext attribute map using the key CURRENT_COMPONENT saving the previous UIComponent associated with CURRENT_COMPONENT for a subsequent call to 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 when pushComponentToEL() and popComponentFromEL() must be called are specified as needed in the javadoc for this class. After pushComponentToEL() returns, a call to getCurrentComponent(javax.faces.context.FacesContext) must return this UIComponent instance until popComponentFromEL() is called, after which point the previous UIComponent instance will be returned from getCurrentComponent()

基本上,这种方法

public void encodeXxx(FacesContext context) {
    try {
        pushComponentToEL(context, this);

        // ...
    }
    finally {
        popComponentFromEL(context);
    }
}

允许您在 // ... 过程中使用 EL 中的 #{component} 或托管 bean 中的 UIComponent#getCurrentComponent() 获取 this 组件。

well known examples 之一是这个构造:

<h:inputText ... styleClass="#{component.valid ? 'valid' : 'error'}" />

其中 #{component.valid} 基本上是指 UIInput#isValid().