Oracle® Application Development Framework Developer's Guide 10g (10.1.3.1.0) Part Number B28967-01 |
|
|
View PDF |
When you drop a method as a command button, JDeveloper binds the button to the execute
method on the associated binding object. This binding allows you to create the JSF page declaratively, without needing to write the associated binding code. However, there may be occasions when you need to add logic before or after the method executes. For example, in order to delete multiple selected rows in a table, you must add code before the delete method executes that accesses each row and makes it current. For more information, see Section 7.6.4, "How to Use the TableSelectMany Component in the Selection Facet".
JDeveloper allows you to add logic to a declarative method by creating a new method and property on a managed bean that provide access to the associated action binding. By default, this generated code executes the method of the corresponding binding. 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 binding. 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 add validation logic before the method is executed, and sets a parameter value after the method is executed.
SRMain.java
: The deleteSR_action
method overrides the removeServiceRequest
method to check whether a service request has associated histories before deleting. The srDelete_action
method overrides the removeServiceHistory
method in order to delete multiple rows in the Service History table.
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 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 10.3, "Creating Command Components to Execute Methods".
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 10-5.
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 10-6. For more information about auto-binding, see Section 4.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 10-6 shows the code inserted into the bean. In this example, a command button is bound to the mergeEntity
method.
Example 10-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("mergeEntity"); 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 10-7 |
Example 10-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 10-8 shows the code that JDeveloper adds to the chosen managed bean. Notice that the return String
"back
" was automatically added to the method.
Example 10-8 Generated Code in a Backing Bean to Access the Binding Object
private BindingContainer bindings; ... public String commandButton1_action() { BindingContainer bindings = getBindings(); OperationBinding operationBinding = bindings.getOperationBinding("mergeEntity"); Object result = operationBinding.execute(); if (!operationBinding.getErrors().isEmpty()) { return null; } return "back"; }
JDeveloper automatically rebinds the UI command component to the new method using the Action
attribute, instead of the ActionListener
attribute. For example, Example 10-9 shows the code on a JSF page for a command button created by dropping the mergeEntity
method. Notice that the actionListener
attribute is bound to the mergeEntity
method, and the action
attribute has a String
outcome of back
. 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 10-9 JSF Page Code for a Command Button Bound to a Declarative Method
<af:commandButton actionListener="#{bindings.mergeEntity.execute}" text="persistEntity" disabled="#{!bindings.persistEntity.enabled}" id="commandButton1" action="back"/>
Example 10-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 10-10 JSF Page Code for a Command Button Bound to an Overridden Method
<af:commandButton text="persistEntity" disabled="#{!bindings.mergeEntity.enabled}" binding="#{backing_create.commandButton1}" id="commandButton1" action="#{backing_create.commandButton1_action}"/>
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 9.4, "Using Dynamic Navigation".
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 |