【问题标题】:JSF - passing parameter to @PostConstruct methodJSF - 将参数传递给@PostConstruct 方法
【发布时间】:2012-12-19 12:02:21
【问题描述】:

我在将参数传递给我的@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 页面)

【问题讨论】:

标签: jakarta-ee jsf-2 ejb


【解决方案1】:

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

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

  • #{profileViewer.showProfile(login)}
  • 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/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-20
    相关资源
    最近更新 更多