2013-01-05 600 views
2

我將一個參數傳遞給@ManagedBean的@PostConstruct方法時遇到了一些問題。我已經知道不能這樣做,但我也不知道如何做。JSF - 將參數傳遞給@PostConstruct方法

讓我們先從一些代碼:

<h:form> 
       <h:dataTable value="#{accountsList.accountsList}" var="konto"> 
        <h:column> 
         <f:facet name="header">#{messages.id}</f:facet> 
         #{konto.id} 
        </h:column> 
        <h:column> 
         <f:facet name="header">#{messages.login}</f:facet> 
         <h:commandLink value="#{konto.login}" action="#{profileViewer.showProfile()}" /> 
        </h:column> 
        ......... 
       </h:dataTable> 
    </h:form> 

上面的XHTML是用來顯示帳戶列表。

看看commandLink。我想將它的值(用戶的登錄名)作爲參數傳遞給作爲ProfileViewer bean的PostConstruct方法的操作方法。

這裏的ProfileViewer bean代碼:

@ManagedBean 
@RequestScoped 
public class ProfileViewer { 

@EJB 
private MokEndpointLocal mokEndpoint; 

private Konta konto; 

private String login; 

@PostConstruct 
public String showProfile(){ 
    konto = mokEndpoint.getAccountByLogin(login); 
    return "profile"; 
} 

public Konta getKonto() { 
    return konto; 
} 

public void setKonto(Konta konto) { 
    this.konto = konto; 
} 

public String getLogin() { 
    return login; 
} 

public void setLogin(String login) { 
    this.login = login; 
} 

public ProfileViewer() { 
} 
} 

我怎樣才能做到這一點?請幫幫我!我將不勝感激一個簡單而好的解決方案和一些代碼的答案。

好的,我會這樣說: 我有一個顯示帳戶列表的JSF頁面。我想每個帳戶名(登錄)是一個鏈接資料信息(這是顯示關於選擇的帳戶信息其他JSF頁面)

+1

你究竟想在這裏獲得什麼?請刪除不必要的變量,使問題更清楚。 – 757071

+0

通過此鏈接http://balusc.blogspot.in/2011/09/communication-in-jsf-20.html。它對JSF-2.0中的通信給出了一個清晰的概念。 – 757071

回答

2

切勿在@PostConstruct方法與視圖參數玩。這只是在構造函數之後調用的,JSF沒有建立它的值。 APPART從,你應該從操作方法去除@PostConstruct註解之後,你可以從h:commandLink通過多種方式在用戶登錄值:

http://www.mkyong.com/jsf2/4-ways-to-pass-parameter-from-jsf-page-to-backing-bean/

  • #{profileViewer.showProfile(登陸) }
  • F:PARAM NAME = 「用戶」 值= 「登錄」
  • F:屬性附加傷害名稱= 「用戶」 值= 「登錄」
  • F:setPropertyActionListener目標= 「#{} profileViewer.showProfile」 值= 「登陸」

要小心,如果聲明#{profileViewer.showProfile(login)},某些服務器可以有這樣的問題:

http://www.mkyong.com/jsf2/how-to-pass-parameters-in-method-expression-jsf-2-0/