JSF2: Handling Data between ManagedBeans
I am confronted with a problem I don't 开发者_高级运维find an easy suificiant way:
I use different ManagedBeans to organise my views. There are controller that manage actions and passes data from/to persistence layer. Further there are models that lie behind the views and contain their data.
Controller which are RequestScoped initialise the model (-managedBean) and fills it with data. Model (-managedBeans) are SessionScoped. That worked well so far.
But how can I pass data between two requestScoped controllers?
My Controller initialises the model and when I want to work with data it is always gone because with any request data in my models are refilled by empty objects.
I would like to extend my question: I'm doing it like Bozho posted it already. But my 'anotherBean' (like he called it) is null after some times? Does JSF recreates beans for some reasons?
You can access the requestscope Map
through the FacesContext
envorinment.
FacesContext.getCurrentInstance().getExternalContext().getRequestMap()
There you have a Map where the string is the managed bean name and the Object is the instance of the managed bean.
I'd recommend you this article about communication in JSF, written by BalusC.
@ManagedProperty(name = "#{anotherBean}")
private AnotherBean anotherBean;
Thank you all for your answers, but my mistake has been somewhere else.
A few weeks ago I tried to integrate PrimeFaces for testing purposes. I didn't get it working and removed it. But I forgot to remove the dependency in my pom file (for maven). After removing PrimeFaces from my pom file, my webapp works fluently.
use ViewScoped for ManagedBean instead of RequsetScope
精彩评论