I need to pass a parameter (POST) to a @managedBean, I used managed properties like this:
@ManagedProperty(value = \"#{param.id}\")
private int id;
>
Two ways:
Make the bean request scoped and inject the view scoped one as another @ManagedProperty.
@ManagedBean
@RequestScoped
public class RequestBean {
@ManagedProperty(value="#{param.id}")
private Integer id;
@ManagedProperty(value="#{viewBean}")
private ViewBean viewBean;
}
The view scoped bean is available during @PostConstruct and action methods of request scoped bean. You only need to keep in mind that the id can get lost when you do a postback to the same view without the parameter.
Or, grab it manually from the request parameter map during bean's initialization.
@ManagedBean
@ViewScoped
public class ViewBean {
private Integer id;
@PostConstruct
public void init() {
id = Integer.valueOf(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("id"));
}
}
This way the initial id is available during the entire view scope.