2013-08-02 30 views
0

我在我的xhtml中有一個p:commandLink,並在「顯示」/「隱藏」之間切換值。 有沒有什麼方法可以從後臺bean獲取此命令鏈接的值? 我的意思是,我想知道命令鏈接當前顯示什麼值,即顯示/隱藏?如何從後臺bean獲取命令鏈接值(顯示名稱)?

+0

相反,你可以在commandLink方法調用中傳遞的參數。 –

+0

如果您需要本地化您的web應用程序,並因此提供不同語言的按鈕標籤,如'value =「#{msg ['button.label']}」'?因此需要知道按鈕的值是絕對不是一個好的解決方案,因爲您必須考慮所有這些本地化值。如果多個按鈕調用相同的方法,我建議重新制定您的問題,而不是詢問如何區分按下的按鈕。 – BalusC

+0

@ BalusC-我同意你和你正確理解我的要求。從命令鏈接我調用和actionListener方法,我想區分它是否目前顯示「顯示」/「隱藏」。因爲基於此,我必須向用戶顯示一個彈出對話框。例如如果它是「顯示」,我希望對話框來。但是,如果「隱藏」,我只想繞過調用此對話框的bean代碼.FYI,我從後端顯示對話框。是否有可能假設有沒有其他語言支持嗎? –

回答

1

爲了這一點,調用部件只是在ActionEvent參數可供選擇:

<h:commandLink id="show" value="Show it" actionListener="#{bean.toggle}" /> 
<h:commandLink id="hide" value="Hide it" actionListener="#{bean.toggle}" /> 

public void toggle(ActionEvent event) { 
    UIComponent component = event.getComponent(); 
    String value = (String) component.getAttributes().get("value"); 
    // ... 
} 

然而,這是一個糟糕的設計。可本地化的文本絕對不應該被用作業務邏輯的一部分。

相反,要麼鉤上部件ID:

String id = (String) component.getId(); 

傳遞方法參數(EL 2.2 or JBoss EL required):

<h:commandLink id="show" value="Show it" actionListener="#{bean.toggle(true)}" /> 
<h:commandLink id="hide" value="Hide it" actionListener="#{bean.toggle(false)}" /> 

public void toggle(boolean show) { 
    this.show = show; 
} 

甚至只是直接調用setter方法,而不需要額外的動作監聽器方法:

<h:commandLink id="show" value="Show it" actionListener="#{bean.setShow(true)}" /> 
<h:commandLink id="hide" value="Hide it" actionListener="#{bean.setShow(false)}" />