JSF - 将参数传递给@PostConstruct方法

时间:2013-01-05 01:14:27

标签: java-ee jsf-2 ejb

将参数传递给@ManagedBean的@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。我想将它的值(用户的登录名)作为参数传递给action方法,该方法是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 个答案:

答案 0 :(得分:2)

永远不要尝试在@PostConstruct方法中使用视图参数。刚刚在构造函数和JSF之后调用它就没有建立值。从那里开始,您应该从操作方法中删除@PostConstruct注释,之后您可以从h:commandLink以多种方式传递用户登录值:

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

  • #{profileViewer.showProfile(登录)}
  • f:param name =“user”value =“login”
  • f:atribute name =“user”value =“login”
  • f:setPropertyActionListener target =“#{profileViewer.showProfile}”value =“login”

如果声明#{profileViewer.showProfile(login)},请注意,某些服务器可能会出现问题:

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