How to get application scoped managed bean on servlet - jsf

I have a bean that I configured to be application scoped in faces-config.xml as below:
<managed-bean eager="true">
<managed-bean-name>communicationCRCList</managed-bean-name>
<managed-bean-class>com.ingdirect.edeal.bean.CommunicationCRCListBean</managed-bean-class>
<managed-bean-scope>application</managed-bean-scope>
</managed-bean>
That been is loaded when the app starts by the call of the INIT() method on a different class.
The app starts, the class is loaded and create the bean calling the init method.
As it's an application scoped bean, it should be accessible from everywhere.
In my case, I would like to get that bean in a servlet. I read that all the application scoped managed beans are loaded in the servletContext. In order to get that bean, I tried the following in my servlet:
CommunicationCRCListBean CommunicationCrcListBean = (CommunicationCRCListBean) getServletContext().getAttribute(COMMUNICATION_LIST_CRC_BEAN);
For information, COMMUNICATION_LIST_CRC_BEAN = communicationCRCList--> name of the bean. Unfortunatly, I get null....
I can't figure out what's wrong... Does somebody has an idea ?

Related

Does JSF Managed Bean scope impacts visibility & access between EAR, WAR and JAR

I have EAR with in Which WAR & few JARS. Eventually few more JARS under my WAR too.
I have packaged a set of DATA OBJECTs inside a EAR as JAR, out which one of its Managed Bean is under 'SessionScope' and with its property -> 'eager = true'. Say 'A.Java'
Now, From My WAR I have a ManagedBean, say 'B.java', with 'RequestScope' trying to get an instance of A.java, Which is returned as NULL. From the Logs, When traced got the below exception:
The managed-bean with name 'B' must be application scoped to support eager=true.
Is there a hierarchy of Managed Bean Scope, that we have to ensure while archiving and deploying as EAR???
Eager Application-Scoped Beans
Managed beans are lazily instantiated. That is, that they are instantiated when a request is made from the application.
To force an application-scoped bean to be instantiated and placed in the application scope as soon as the application is started and before any request is made, the eager attribute of the managed bean should be set to true as shown in the following example:
#ManagedBean(eager=true)
#ApplicationScoped
The eager property means that the container creates the instance at application startup and not on demand. It can put this instance just into the application scope. (There are no other scopes at this time). So the eagerly created managed beans must be ApplicationScoped.

How can I get all view scope managed beans for the current ViewRoot?

I have to implement some bean post processor for all view scope managed beans on the page. I have implemented SystemEventListener with PostConstructViewMapEvent as system-event-class. How can I get all view scope managed beans for the current ViewRoot? viewRoot.getViewMap() returns empty map (Mojarra JSF 2.1.19), but I have a few managed beans with view scope. Of course I can invoke my post processor in #PostConstruct method in every such bean, but is there a global solution like in Spring Framework?
My faces-config.xml configuration:
<application>
<system-event-listener>
<system-event-listener-class>com.myapplicationname.ViewScopeBeanPostProcessor</system-event-listener-class>
<system-event-class>javax.faces.event.PostConstructViewMapEvent</system-event-class>
<source-class>javax.faces.component.UIViewRoot</source-class>
</system-event-listener>
</application>

How to initialise a Managed Bean in request scope from a servlet

I have a servlet that will navigate to a JSF page upon some conditions using ExternalContext.redirect.
I need to initialise a Managed Bean in this Servlet and set it in request scope so that my JSF page can directly access the Managed Bean's property and display them on the page load.
I had seen posts that sets the bean using getServletContext()
Like,
getServletContext().setAttribute("beanName",new Bean())
it works.But this approach will be setting the bean in application scope instead of request scope.
Also i tried the following:
request.setAttribute("beanName",new Bean())
it doesnt work
So pls let me know if there is any way to set /initialise a managed bean in a request scope
If it's a request scoped bean, use HttpServletRequest#setAttribute()
BeanName beanName = new BeanName();
request.setAttribute("beanName", beanName);
If its a session scoped bean,
request.getSession().setAttribute("beanName", beanName);

How to use HTTP Session Scope in portlets ?

In my application there are 5 portlets accessing same bean class which is in session scope. My problem is, whenever I open a portlet, managed bean initializes. Managed bean should be initialize once in a session. In my case bean initializing 5 times. Can anyone tell me what is the root cause of that problem?
Here is my bean :
#ManagedBean(name="userManagementBean")
#SessionScoped
public class UserManagementBean {
public UserManagementBean() {
System.out.println("In getter setter bean");
sName=userManagementHelper.findScreenName();
directReport=new DualListModel<String>();
addUserToGroupDual=new DualListModel<String>();
addUserToGroupDual.getSource().clear();
addUserToGroupDual.getTarget().clear();
............
When you annotate your beans with #SessionScoped in Portlet application it is mapped to something as "Portlet Instance Session". This means that this bean will live in session of that portlet, and each portlet has its own session. There is something called "Global Session" which is session shared across all portlets, but as far as I know there is not such annotation in JSF.
JSR286 has user session based scope but it would depend on your portal server if it has the implementation for this as a Custom Scope for JSF.
I know for sure that Websphere portal 8.x supports this.
In Websphere portal 8.x you can specify your managed bean like,
#ManagedBean(name="userManagementBean")
#CustomScoped("#{portletApplicationSessionScope}")
public class UserManagementBean {
...
}
Have a look into your portal server documentation to see if it supports that.
You can use Apache JSF portlet bridge as you have updated that you are using liferay ,
It will expose Application Session Scope as EL,
Add it to application scope in portlet A
PortletSession session = request.getPortletSession();
session.setAttribute("name",name.getValue().toString(),PortletSession.APPLICATION_SCOPE);
and use in portlet B
PortletSession session = request.getPortletSession();
String value = session.getAttribute("name", PortletSession.APPLICATION_SCOPE).toString();
Your xhtml ,
<h:inputText id="itName"
required="true"
value="#{httpSessionScope.name}"/>

JSF2.0 PostConstructApplicationEvent managed bean is null

We have JSF2.0 in Tomcat6.0 , need to initialize a ApplicationScope Bean while web server is started.
I tried using the PostConstructApplicationEvent processEvent method to initialize the Bean , but the managed bean from faces-config.xml is returning null.
Is there any other better way to instantiate the bean after startup?
Remove any faces-config.xml declarations related to the bean (they will otherwise override the JSF 2.0 annotations) and then annotate the bean with #ManagedBean(eager=true) as follows:
#ManagedBean(eager=true)
#ApplicationScoped
public class Bean {
// ...
}
This way the bean will always be instantiated on JSF webapp startup, without the need to view any page. You can then do the initialization job in the constructor and/or #PostConstruct of the bean.

Resources