2

我正在研究一个现有的 JSF 组件,该组件的encodeEnd方法以:

// popComponentFromEL(context);

JavadocUIComponent#popComponentFromEL(FacesContext context)告诉我:

UIComponent从属性映射中弹出当前组件,FacesContext以便前一个UIComponent(如果有)成为当前组件。

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

我发现同一个库中的其他组件都没有使用它。

4

1 回答 1

2

那是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 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()

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().

于 2017-05-20T20:20:41.553 回答