3

我在将参数传递给我的@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。我想将它的值(用户的登录名)作为参数传递给作为 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 页面)

4

1 回答 1

2

永远不要尝试在@PostConstruct方法中使用视图参数。这是在构造函数之后调用的,而 JSF 没有在其上建立值。除此之外,您应该@PostConstruct从操作方法中删除注释,然后您可以通过多种方式从 a 传递用户登录值h:commandLink

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

  • #{profileViewer.showProfile(登录)}
  • f:param 名称="用户" 值="登录"
  • f:属性名=“用户”值=“登录”
  • 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/

于 2013-01-05T02:11:08.393 回答