I want to invoke a managed bean method in a Servlet.
And so I used the following code:
I am able to get the value when the page from where the action has been initiated is not mapped to the backing bean's navigation rule.
But in some cases where the page from where the action has been initiated is mapped to the backing bean's navigation rule, I am unable to access the backing bean.
I get a NULL value for the object of the backing bean.
The scope of the backing bean is set to SESSION and I do not evict it explicitly.
JSF managed beans are constructed on demand, not in advance. So if you attempt to access a managed bean but that bean hasn't yet been accessed by a JSF request, it won't exist even though it's defined in faces-config.
There are really only 2 ways to handle that.
1. Ensure that a JSF request is ALWAYS made first
2. Manually construct the bean in servlet code if it doesn't exist yet.
Solution #2 does have one downside, though. The JSF bean manager also injects the managed properties when it constructs the bean. If you instantiate the bean in servlet code, you also have to manually inject the managed properties. Which means that you have to keep that code in sync with the management specs in faces-config or unpredictable things may happen.
The secret of how to be miserable is to constantly expect things are going to happen the way that they are "supposed" to happen.
You can have faith, which carries the understanding that you may be disappointed. Then there's being a willfully-blind idiot, which virtually guarantees it.
I think she's lovely. It's this tiny ad that called her crazy:
a bit of art, as a gift, the permaculture playing cards