2013-08-16 52 views
1

我實現了一個簡單的方法,即迭代JSF組件樹並將組件設置爲禁用。 (所以用戶不能更改這些值)。但是這種方法不適用於複合組件。我如何至少檢測一個複合組件?然後我可以嘗試將特殊屬性設置爲禁用。如何在JSF組件樹中查找組合樹?

+0

由於對複合組件支持bean類型的唯一限制是實現'NamingContainer',所以您無法檢測到某件事是否爲複合件,但還有很多其他的東西正在實現該接口。 – rdcrng

+0

請問每個問題一個問題。不要改變現有的問題。它使答案不完整甚至無效。 – BalusC

+0

@BalusC對不起,這是我的第一個問題,關於安全性的想法在我想到這個時候會出現。你可以重新創建你的答案嗎?你的回答非常有用。 – Tony

回答

2

UIComponent類有一個isCompositeComponent()輔助方法正是爲了這個目的。

所以,這應該只是做:

for (UIComponent child : component.getChildren()) { 
    if (UIComponent.isCompositeComponent(child)) { 
     // It's a composite child! 
    } 
} 

對於有意運作 「在幕後」,這裏是從鑽嘴魚科2.1.25執行源代碼:

public static boolean isCompositeComponent(UIComponent component) { 

    if (component == null) { 
     throw new NullPointerException(); 
    } 
    boolean result = false; 
    if (null != component.isCompositeComponent) { 
     result = component.isCompositeComponent.booleanValue(); 
    } else { 
     result = component.isCompositeComponent = 
       (component.getAttributes().containsKey(
          Resource.COMPONENT_RESOURCE_KEY)); 
    } 
    return result; 

} 

因此通過存在具有Resource.COMPONENT_RESOURCE_KEY定義的名稱的組件屬性來識別,其值爲"javax.faces.application.Resource.ComponentResource"

+0

謝謝你的幫助!但是現在我正在考慮安全問題。我編輯了我的問題。 – Tony

+0

指向我的安全問題的鏈接[安全問題JSF。服務器端的禁用屬性](http://stackoverflow.com/questions/18274010/security-issues-jsf-disabled-attribute-on-server-side) – Tony