I need to pass a parameter (POST) to a @managedBean, I used managed properties like this:
@ManagedProperty(value = "#{param.id}")
private int id;
And the scope of the Bean is ViewScope
I end up with this error:
Unable to create managed bean receipt. The following problems were found: - The scope of the object referenced by expression #{param.id}, request, is shorter than the referring managed beans scope of view
What can I do?
arjan take a look:
My page: Facelet Title
<form method="post" action="faces/index.xhtml">
<input name="id" value="4" />
<input type="submit" value="submit" />
</form>
<h:form>
<h:commandLink value="click" action="index">
<f:param id="id" name="id" value="20"/>
</h:commandLink>
</h:form>
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.