I’ve got a little problem with passing a parameter to @PostConstruct method of my @ManagedBean. I already know it can’t be done just like that, but I also don’t know how it can be done.
Let’s start with some code:
<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>
The xhtml above is used to display accounts list.
Take a look at the commandLink. I would like to pass it’s value (user’s login) as a parameter to the action method which is a PostConstruct method of ProfileViewer bean.
Here’s the ProfileViewer bean code:
@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() {
}
}
How can I do this? Please help me! I would appreciate an answer with an easy and good solution and some code.
Ok, I’ll say it this way:
I’ve got a JSF page displaying list of accounts. I want each account name (login) to be a link to profile info (which is other jsf page displaying info about selected account)
Never try to play with view parameters in
@PostConstructmethod. That’s being called just after constructor and JSF doesn’t have established values on it. Appart from that, you should remove@PostConstructannotation from the action method and after that you can pass the user login value in multiple ways from ah:commandLink:http://www.mkyong.com/jsf2/4-ways-to-pass-parameter-from-jsf-page-to-backing-bean/
Be careful if declaring
#{profileViewer.showProfile(login)}, some servers can have problems with that:http://www.mkyong.com/jsf2/how-to-pass-parameters-in-method-expression-jsf-2-0/