Saturday, 15 February 2014

How to pass values between pages within a task flow and other task flow using pageFlow and request scope

I have scenario to pass value from one page to another page within a task flow or may be other task flow.
ADF provides some scopes for example, pageFlow, request, session and application. Using some of these scopes we can hold the value of variable throughout the application.

In this post we will see how to use pageFlow and request scope. 

pageFlow scope is used to pass parameter between the pages within a task flow. 
request scope is used to pass parameter to another page which is there in different task flow.

I have 2 pages in a default unbounded task flow(first task flow), page1 and page2. I have created one bounded task flow(second task flow) which has one page fragment ie page3.

Unbounded Task Flow
 Bounded task Flow


Create the pages by double clicking on each node in the task flows.
Two pages and one fragment gets created
page1.jspx(or .jsf)
page2.jspx(or .jsf)
page3.jsff


In page1 i have one text field(username) and one button. When i click on submit button page2 will be shown.
Page 2 has one output text field to show the value entered in the page1 and one region. The region contains the contents of the other task flow(ie page3).

In the page1 bind the username to some existing managed bean, if not create new one and bind it(i have created one with the name testScopeBean and make sure that the bean scope is pageFlow in the task floew ie adfc-config.xml). It will create getter and setter methods. And create one instance variable where you want to store the value of the username. And generate the getter and setter methods for the instance variable(scopeVariable)




On click of submit button call the actionListner and store the value of username into the scopeVariable in the testScopeBean.
Managed bean code is show below.


public class TestScopeBean {
    private RichInputText username;
    private String scopeVariable;

    public TestScopeBean() {
    }

    public void setUsername(RichInputText username) {
        this.username = username;
    }

    public RichInputText getUsername() {
        return username;
    }
    
    public void setScopeVariable(String intVar) {
        this.scopeVariable = intVar;
    }

    public String getScopeVariable() {
        return scopeVariable;
    }

    public void getUserName(ActionEvent actionEvent) {
        // Add event code here...
        RichInputText userName = (RichInputText)FacesContext.getCurrentInstance().getViewRoot().findComponent("it1");
//it1 is the id of input text field
        String userName1 = (String)userName.getValue();
        System.out.println("user name "+userName1);
        setScopeVariable(userName1);
    }
}

Now you can use the scopeVariable in the other pages within a task flow like
af:outputText value="pageFlow scope variable #{pageFlowScope.testScopeBean.scopeVariable}"  id="ot1"/>

Run the page1.jspx and enter the user name and click on the submit button you can the result in page2.jspx.

You use the same pageFlow scope variable in other task flow also, but it will show blank value. Because pageFlow Scope value is restricted to only within task flow







Now change the managed bean scope to request in the adfc-config.xml managed bean tab.
The request Scope variable can be access like #{testScopeBean.scopeVariable}
Change the the wherever you have used the #{pageFlowScope.testScopeBean.scopeVariable} to #{testScopeBean.scopeVariable}.

Run the page1.jspx again and see the result, now it will show value in other task flow also.










4 comments:

  1. Hi Basavaraj Petlur,

    we need to use any set action listener in this case why because when am i try this one it is not working properly

    Thanks,
    Raghu

    ReplyDelete
  2. Hi,

    If we use this method to pass value to second page, when we are coming back to the 1st page and we try again to go to 2nd page this does not work.

    ReplyDelete