Passing parameters to managed bean methods using EL

No, you cannot pass arguments to a managed bean method using Expression Language. But you can work around this limitation. To make required arguments available to a managed bean method, you either use in memory attributes, the ADF binding layer or a setter/getter method on a managed bean that sets an internal variable.

Sample setup: The managed bean method onCallManagedBean used in the following is referenced from a command button action property. The page also contains an ADF bound table that I use to access the department name of the current selected row to pass it as an argument to the managed bean method.

Option 1: Call a managed bean method to set properties

In this use case, the managed bean itself has a propertydepartment defined, which value can be set through a pair of setter/getter methods.

To pass the department name of the selected table row to the managed bean in the sample below, I created an attribute binding for DepartmentName.

Then, in an af:setPropertyListener, I referenced the department name attribute binding using Expression Language, to pass its value to the managed bean property department. The department property is a private variable in the managed bean that is exposed by a public setter/getter method pair.

The managed bean code is shown below:

 String department = "";     
  public DepartmentsBean() {}
  public String onCallManagedBean() {
   System.out.println("The Department Name is "+department);
   return null;
}

public void setDepartment(String department) {
  this.department = department;
}

public String getDepartment() {
  return department;
}

 

Option 2: Use memory attributes to pass arguments to a managed bean

Option 2 is similar to Option 1, except for that it does not use a property in the managed bean to hold the argument required by the invoked method, but a memory attribute.

The recommendation when using memory attributes is to go with the shortest scope so that whatever data is stored in memory gets cleared out at earliest opportunity. In the use case of passing an argument to a managed bean method, request scope is good enough to use. For most of the requirements for passing data in ADF Faces, you don't need to go larger than request scope or view scope.

(!) Note that the prefix "requestScope" is only needed when writing directly into a memory scope. If you access a managed bean in the same scope then this prefix should not be used. Managed beans are only referenced with a scope prefix if they are defined in backingBeanScope, viewScope or pageFlowScope as these are custom ADFc scopes. For servlet scopes like request, session and application, using a prefix will cause NPE for when you access a managed bean that hasn't been instantiated before. So for all regular servlet scopes, don't use a prefix when accessing managed beans.

When clicking the command button, the department name that is read from the binding layer is copied into the departmentNameTmpmemory attribute. To access this property in a managed bean, assuming you use ADF, the ADFContext object is used.

 public String onCallManagedBean() {       
   String department = "";       
   ADFContext adfContext = ADFContext.getCurrent();
   Map requestScope = adfContext.getRequestScope();
   department = (String) requestScope.get("departmentNameTmp");       
   System.out.println("The Department Name is "+department);
   return null;
 }

If you are not in an ADF environment, then the request scope Map is accessible from the FacesContext | ExternalContext in JavaServer Faces

Option 3: Access the ADF binding layer to obtain the required information

If the data used by the managed bean method comes from the ADF binding, as in this sample, then you can also directly access the binding layer without copying the values temporarily somewhere else. The two option explained earlier are good to use if the arguments to be passed to a managed bean method don't come from the binding layer but from somewhere else.

 public String onCallManagedBean() {

   String department = "";
   BindingContext bctx = BindingContext.getCurrent();
   BindingContainer bindings = bctx.getCurrentBindingsEntry();
   AttributeBinding departmentNameBinding =
              (AttributeBinding) bindings.get("DepartmentName");
   department = (String) departmentNameBinding.getInputValue();
   System.out.println("The Department Name is "+department);
   return null;
 }

The managed bean code accesses the ADF binding layer through the BindingContext class that is a runtime representation of the DataBindings.cpx file. It looks up the current binding container to access the value binding defined for the DepartmentName.

In summary: Expression Language in JavaServer Faces 1.2 does not support passing arguments to methods in a managed bean. Not a big deal though as you can pass the required information using memory attributes, managed bean properties of the ADF binding layer.

Note: The monthly OTN harvest summary for April 2011, published on ADF Code Corner (available in May), contains this sample with images for you to follow the sample.

http://www.oracle.com/technetwork/developer-tools/adf/learnmore/index-101235.html

Comments:

Post a Comment:
Comments are closed for this entry.
About

The Oracle JDeveloper forum ranks in the Top 5 of the most active forums on the Oracle Technology Network (OTN).



The OTN Harvest blog is a summary of selected topics posted on the OTN Oracle JDeveloper forum.



It is an effort to turn knowledge exchange into an interesting read for developers who enjoy little nuggets of wisdom





Frank Nimphius

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today