Thursday, May 24, 2012

Dynamic Control Flow Case using ActionEvent

Here is a scenario where we need to navigate from one page to another page based on some condition. In design time set the 'Action' property of UI component can be used to navigate between pages, but here Action property can't be used since navigation is based on condition. In this article will see how can we programmatically handle the control flow case in managed bean using ActionEvent.

This can be achieved using handleNavigation method from NavigationHandler class. But direct invocation of methods on the NavigationHandler doesn't conform with JSF specification and it bypasses JSF lifecycle invoke application phase, also pending changes may not be submitted.

Take an example, here we have DeptBrowsePage which display departments table with multiple select option enabled and with edit button. If user selects the single row and click on edit Control Flow Case should go to DeptFormPage and if multiple rows selected it should go to DeptTablePage.

You can download the sample workspace from here
[Runs with Oracle JDeveloper 11.1.2.0.0(11g R2) + HR Schema]

The following code illustrates the Control Flow Case dynamically at run time using handleNavigation method. Please download the application to see the entire scenario.

Note: - Here created the bindings for buttons DeptForm, DeptButton respective with getDeptFormButton(), getDeptTableButton() and buttons rendered property made false.
/**
* In this method first looking for the selected row keys
* Based on the condition, queue the action event on respective command button
* @return
*/
public String navigateHanlderMethod() {
        //Get the selected rowkey values
        RowKeySet selectedDepts = getDeptTable().getSelectedRowKeys();
        if (selectedDepts.size() == 1) { //If Single row is selected
            ActionEvent actionEvent = new ActionEvent(this.getDeptFormButton());
            actionEvent.queue();
        } else if (selectedDepts.size() > 1) { // If multiple rows are selected
            List selectedRowKeys = getSelectedRowKeys();
            Map pfScope = ADFContext.getCurrent().getPageFlowScope();
            pfScope.put("deptList", selectedRowKeys);
            ActionEvent actionEvent = new ActionEvent(this.getDeptTableButton());
            actionEvent.queue();
        }
        return null;
 }

Output: Run the index.jspx page.


Select single row and click on the Edit button should take to DeptFormPage.jspx and display only selected row.


Select multiple rows and click on the Edit button should take to DeptTablePage.jspx and display only selected rows.

No comments:

Post a Comment