Environment (JDeveloper 11.1.2.0.0)
Lately we had a case in which we needed to access an instance of pageFlowScope bean from a requestScope bean, this is a common use case and many developers may need to do the same. In this post I will explain how to do this using two options.
Firstly suppose we have two java classes one of them registered as pageFlowScope bean and the other is registered as requestScope bean as shown below.
package view.managed; public class PageFlowScopeClass { public PageFlowScopeClass() { super(); } private String someData; public void setSomeData(String someData) { this.someData = someData; } public String getSomeData() { return someData; } }
package view.managed; public class RequestScopeClass { public RequestScopeClass() { super(); } }
both classes are registered in adfc-config.xml file
<?xml version="1.0" encoding="windows-1252" ?> <adfc-config xmlns="http://xmlns.oracle.com/adf/controller" version="1.2"> <managed-bean id="__1"> <managed-bean-name>PageFlowScopeBean</managed-bean-name> <managed-bean-class>view.managed.PageFlowScopeClass</managed-bean-class> <managed-bean-scope>pageFlow</managed-bean-scope> </managed-bean> <managed-bean id="__2"> <managed-bean-name>RequestScopeBean</managed-bean-name> <managed-bean-class>view.managed.RequestScopeClass</managed-bean-class> <managed-bean-scope>request</managed-bean-scope> </managed-bean> </adfc-config>
The first option to access an instance of PageFlowScopeBean from RequestScopeBean is by adding a managed property to the RequestScopeBean, and here are the steps to do:
- open the adfc-config.xml file, click on Overview menu, select the Managed Beans node, select RequestScopeBean and click the add managed property icon as shown below.
- enter the name,class,and the value for the property in our case these field should be:
Name: myPageFlowBean
Class: view.managed.PageFlowScopeClass
value: #{pageFlowScope.PageFlowScopeBean}
Now adfc-config.xml will be something like this<?xml version="1.0" encoding="windows-1252" ?> <adfc-config xmlns="http://xmlns.oracle.com/adf/controller" version="1.2"> <managed-bean id="__1"> <managed-bean-name>PageFlowScopeBean</managed-bean-name> <managed-bean-class>view.managed.PageFlowScopeClass</managed-bean-class> <managed-bean-scope>pageFlow</managed-bean-scope> </managed-bean> <managed-bean id="__2"> <managed-bean-name>RequestScopeBean</managed-bean-name> <managed-bean-class>view.managed.RequestScopeClass</managed-bean-class> <managed-bean-scope>request</managed-bean-scope> <managed-property id="__5"> <property-name>myPageFlowBean</property-name> <property-class>view.managed.PageFlowScopeClass</property-class> <value>#{pageFlowScope.PageFlowScopeBean}</value> </managed-property> </managed-bean> </adfc-config>
- in RequestScopeClass create an instance variable of type PageFlowScopeClass with the same name that exists in managed properties and generate accessors; a getter and a setter for this variable. Now the RequestSCopeClass should be
package view.managed; public class RequestScopeClass { private PageFlowScopeClass myPageFlowBean; public RequestScopeClass() { super(); } public PageFlowScopeClass getMyPageFlowBean() { return myPageFlowBean; } public void setMyPageFlowBean(PageFlowScopeClass myPageFlowBean) { this.myPageFlowBean = myPageFlowBean; } }
- now you can get the PageFlowScopeBean instance by calling getMyPageFlowBean() method.
The other option to access an instance of PageFlowScopeBean is to use ExpressionFactory class as shown below
package view.managed; import javax.el.ELContext; import javax.el.ExpressionFactory; import javax.el.ValueExpression; import javax.faces.application.Application; import javax.faces.context.FacesContext; public class RequestScopeClass { public RequestScopeClass() { super(); } public PageFlowScopeClass getInstanceOfPageFlowScopeBean() { FacesContext fctx = FacesContext.getCurrentInstance(); Application application = fctx.getApplication(); ExpressionFactory expressionFactory = application.getExpressionFactory(); ELContext context = fctx.getELContext(); ValueExpression createValueExpression = expressionFactory.createValueExpression(context, "#{pageFlowScope.PageFlowScopeBean}",PageFlowScopeClass.class); PageFlowScopeClass pageFlowScopeInstance =(PageFlowScopeClass) createValueExpression.getValue(context); return pageFlowScopeInstance; } }
to get an instance of PageFlowScopeBean all you need is just to call getInstanceOfPageFlowScopeBean() method.
Interesting article.
Bad thing for ADF is that it does not support DI through Annotations. Eg. in JSF 2.0 you could inject one bean into the other with one annotation. That’s it. No adf-config.xml configuration. No accessing the bean through the context.
Do you happen to know when ADF is going to adopt JSF 2.0 techniques. 11.1.2.0 are said to be JSF 2.0 ready. Have you tried it out?
Thanx in advance.
Spyros
As you said 11.1.2.0 adopts JSF 2.0. But I did not try it.
Hi Jabr,
This is brilliant article. However, I dont know why I cant get it to Work. Iv followed your steps but in vain. I have a pageFlowScope bean which Im invoking from a requestScope bean. See below my request scope and let me know where im wrong
public navigate getInstanceOfPageFlowScopeBean() {
FacesContext fctx = FacesContext.getCurrentInstance();
Application application = fctx.getApplication();
ExpressionFactory expressionFactory = application.getExpressionFactory();
ELContext context = fctx.getELContext();
ValueExpression createValueExpression =
expressionFactory.createValueExpression(context, “#{pageFlowScope.navigate}”, navigate.class);
navigate pageFlowScopeInstance = (navigate)createValueExpression.getValue(context);
return pageFlowScopeInstance;
}
Hello Eric,
I don’t see any mistake in your method.
Did you try the first option?
Are your beans exist in the same task flow?
I tried both the option but i am getting null for both the method.
Hello Pinal,
I tried these two option successfully, may be you made some mistake which result in null value.
HI Jabr.. i have requirement, I have EmpBean that was added in task flow as view scope and its having getEmplist() method list of employees, once user search for employees, i do search and creating a new list, clearing the existing list and adding the search results list.
here is the problem is, when user search again, the viewScopeBean not maintaining the orginal list, it is returning the newly created list, but i want to search again from orginal list.
i knw by creating an another list to save orginal values, and i can use when ever I want getOriginalList().
is it possible to managing the OrginalList data, when ever i want, in taskflow?
Hello,
Please put your question on Jdeveloper and ADF Forum.
https://forums.oracle.com/forums/forum.jspa?forumID=83