Oracle® Application Development Framework Developer's Guide For Forms/4GL Developers 10g (10.1.3.1.0) Part Number B25947-01 |
|
|
View PDF |
When you drop an operation or method as a command button, JDeveloper binds the button to the execute
method for the operation or method. However, there may be occasions when you need to add logic before or after the logic executes. For example, after the Delete
operation successfully executes, you may want to execute the Commit
operation, so that the user does not have to click another button.
JDeveloper allows you to add logic to a declarative operation by creating a new method and property on a managed bean that provide access to the binding container. By default, this generated code executes the operation or method. You can then add logic before or after this code. JDeveloper automatically binds the command component to this new method instead of the execute
property on the original operation or method. Now when the user clicks the button, the new method is executed.
Following are some of the instances in the SRDemo application where backing beans contain methods that inject the binding container and then add logic before or after the declarative method is executed:
SRCreateConfirm.java
: The createSR_action
method overrides the createServiceRequest
method to set the service request's ID as a parameter value on the JSFUtils
helper class after the method is executed.
SRMain.java
: The onDeleteRequest
method overrides the Delete
operation. It first executes the Delete
action. If any errors occur, it redisplays the page. If there are no errors, it executes the Commit
operation on the application module. If any errors occur, it redisplays the page. If there are no errors, the user is returned to the page that is held as the ReturnNavigationRule
value on the UserState
managed bean.
In order to override a declarative method, you must have a managed bean to hold the new method to which the command component will be bound. If your page has a backing bean associated with it, JDeveloper adds the code needed to access the binding object to this backing bean. If your page does not have a backing bean, JDeveloper asks you to create one.
Note: You cannot use the following procedure if the command component currently has an EL expression as its value for theAction attribute, as JDeveloper will not overwrite an EL expression. You must remove this value before continuing. |
To override a declarative method:
Drag the operation or method to be overridden onto the JSF page and drop it as a UI command component.
Doing so creates the component and binds it to the associated binding object in the ADF Model layer using the ActionListener
attribute on the component.
For more information about creating command components using methods on the Data Control Palette, see Section 17.3, "Creating Command Components to Execute Methods".
For more information about creating command components from operations, see Section 13.4.2, "What Happens When Command Buttons Are Created Using the Data Control Palette"
On the JSF page, double-click on the component.
In the Bind Action Property dialog, identify the backing bean and the method to which you want to bind the component using one of the following techniques:
If auto-binding has been enabled on the page, the backing bean is already selected for you, as shown in Figure 17-4.
To create a new method, enter a name for the method in the Method field, which initially displays a default name.
OR
To use an existing method, select a method from the dropdown list in the Method field.
Select Generate ADF Binding Code.
If the page is not using auto-binding, you can select from an existing backing bean or create a new one, as shown in Figure 17-5. For more information about auto-binding, see Section 11.5.4, "How to Use the Automatic Component Binding Feature".
Click New to create a new backing bean. The Create Managed Bean dialog is displayed. Use this dialog to name the bean and the class, and set the bean's scope.
OR
Select an existing backing bean and method from the dropdown lists.
After identifying the backing bean and method, click OK in the Bind Action Property dialog
JDeveloper opens the managed bean in the source editor. Example 17-6 shows the code inserted into the bean. In this example, a command button is bound to the Delete operation.
Example 17-6 Generated Code in a Backing Bean to Access the Binding Object
public BindingContainer getBindings() { return this.bindings; } public void setBindings(BindingContainer bindings) { this.bindings = bindings; } public String commandButton_action1() { BindingContainer bindings = getBindings(); OperationBinding operationBinding = bindings.getOperationBinding("Delete"); Object result = operationBinding.execute(); if (!operationBinding.getErrors().isEmpty()) { return null; } return null; }
You can now add logic either before or after the binding object is accessed.
Tip: To get the result of an EL expression, you need to use theValueBinding class, as shown in Example 17-7 |
Example 17-7 Accessing the Result of an EL Expression in a Managed Bean
FacesContext fc = FacesContext.getCurrentInstance(); ValueBinding expr = fc.getApplication(). createValueBinding("#{bindings.SomeAttrBinding.inputValue}"); DCIteratorBinding ib = (DCIteratorBinding) expr.getValue(fc);
In addition to any processing logic, you may also want to write conditional logic to return one of multiple outcomes depending on certain criteria. For example, you might want to return null
if there is an error in the processing, or another outcome value if the processing was successful. A return value of null
causes the navigation handler to forgo evaluating navigation cases and to immediately redisplay the current page.
Tip: To trigger a specific navigation case, the outcome value returned by the method must exactly match the outcome value in the navigation rule in a case-sensitive way. |
The command button is now bound to this new method using the Action
attribute instead of the ActionListener
attribute. If a value had previously existed for the Action
attribute (such as an outcome string), that value is added as the return for the new method. If there was no value, the return is kept as null
.
When you ovrerride a declarative method, JDeveloper adds a managed property to your backing bean with the managed property value of #{bindings}
(the reference to the binding container), and it adds a strongly-typed bean property to your class of the BindingContainer
type which the JSF runtime will then set with the value of the managed property expression #{bindings}
. JDeveloper also adds logic to the UI command action method. This logic includes the strongly-typed getBindings()
method used to access the current binding container.
Example 17-8 shows the code that JDeveloper adds to the chosen managed bean. Notice that the return String
"Complete
" was automatically added to the method, as that was the value of the action
attribute.
Example 17-8 Generated Code in a Backing Bean to Access the Binding Object
private BindingContainer bindings; ... public String createSRButton_action() { BindingContainer bindings = getBindings(); OperationBinding operationBinding = bindings.getOperationBinding("createServiceRequest"); Object result = operationBinding.execute(); if (!operationBinding.getErrors().isEmpty()) { return null; } return "Complete"; }
This code does the following:
Finds the binding for the associated method, and executes it
Adds a return for the method that can be used for navigation. By default the return is null
, or if an outcome string had previously existed for the button's Action
attribute, that attribute is used as the return value. You can change this code as needed. For more information about using return outcomes, see Section 16.4, "Using Dynamic Navigation".
JDeveloper automatically rebinds the UI command component to the new method using the Action
attribute, instead of the ActionListener
attribute. For example, Example 17-9 shows the code on a JSF page for a command button created by dropping the createServiceRequest
method. Notice that the actionListener
attribute is bound to the createServiceRequest
method, and the action
attribute has a String
outcome of Complete
. If the user were to click the button, the method would simply execute, and navigate to the page defined as the toViewId
for this navigation case.
Example 17-9 JSF Page Code for a Command Button Bound to a Declarative Method
<af:commandButton actionListener="#{bindings.createServiceRequest.execute}" text="createServiceRequest" disabled="#{!bindings.createServiceRequest.enabled}" id="createSRButton" action="Complete"/>
Example 17-10 shows the code after overriding the method on the page's backing bean. Note that the action
attribute is now bound to the backing bean's method.
Example 17-10 JSF Page Code for a Command Button Bound to an Overridden Method
<af:commandButton text="createServiceRequest" disabled="#{!bindings.createServiceRequest.enabled}" id="createSRButton" action="#{backing_SRCreateConfirm.createSRButton_action}"/>
Tip: If when you click the button that uses the overridden method, you receive this error:
it is because the managed bean that contains the overriding method has a scope that is greater than |