2013-01-05 359 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/