簡體   English   中英

如何使用JSF-1.2制作標簽提供程序?

[英]How can I make a Label Provider using JSF-1.2?

我想將頁面中的UIInput組件的標簽屬性自動設置為我已經放入的HtmlOutputLabel組件,就像PrimeFaces開發人員所做的那樣: http : //cagataycivici.wordpress.com/2011/02/11/label-provider- for-jsf-input-components /只有他使用System Events(僅在JSF-2.0中可用的功能)完成了此操作,而我的應用程序在JSF 1.2中。

是否可以使用JSF-1.2(可能帶有相位監聽器)來實現? 缺點是什么?

提前致謝!

更新:這是我迄今為止對相位監聽器的嘗試:

@Override
public void beforePhase(PhaseEvent event) {
    System.out.println("REGISTERING Label Provider");
    FacesContext context = event.getFacesContext();
    List<UIComponent> components = context.getViewRoot().getChildren();
    for (UIComponent uiComponent : components) {
        if (uiComponent instanceof HtmlOutputLabel) {
            HtmlOutputLabel outputLabel = (HtmlOutputLabel) uiComponent;
            System.out.println("CONFIGURING LABEL: " + outputLabel.getId());
            UIComponent target = outputLabel.findComponent(outputLabel
                    .getFor());
            if (target != null) {
                target.getAttributes().put("label", outputLabel.getValue());
            }
        }
    }
}

@Override
public PhaseId getPhaseId() {
    // Only listen during the render response phase.
    return PhaseId.RENDER_RESPONSE;
}

當我訪問視圖時,它從不打印“配置標簽”部分。 驗證uiComponent是否為HtmlOutputLabel的正確測試是什么? 還是我做錯了什么?

UIViewRoot#getChildren()僅返回視圖根的直接子級,而不是您期望的所有子級。 您還需要遞歸地遍歷每個孩子的孩子。

像這樣:

@Override
public void beforePhase(PhaseEvent event) {
    applyLabels(event.getFacesContext().getViewRoot().getChildren());
}

private static void applyLabels(List<UIComponent> components) {
    for (UIComponent component : components) {
        if (component instanceof HtmlOutputLabel) {
            // ...
        } else {
            applyLabels(component.getChildren()); // Reapply on its children.
        }
    }
}

從JSF 2.0開始,上面的方法通過遵循訪問者模式的UIComponent#visitTree()方法得到了方便,因此您只需進行一次調用即可。

<h:outputText value="#{yourVO.tax}" /> 

在操作類中創建一個數據傳輸對象(DTO)對象,並在DTO中聲明一個字符串變量tax,以便您可以使用它在操作類中設置標簽。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM