How to Use EL Expressions Within Managed Beans

Getting Started with ADF Faces 2-29 elFactory.createValueExpressionelContext, expression, Object.class; return valueExp.getValueelContext; } Example 2–19 shows how you can resolve a method expression. Example 2–19 Resolving a Method Expression from a Managed Bean public static Object resloveMethodExpressionString expression, Class returnType, Class[] argTypes, Object[] argValues { FacesContext facesContext = getFacesContext; Application app = facesContext.getApplication; ExpressionFactory elFactory = app.getExpressionFactory; ELContext elContext = facesContext.getELContext; MethodExpression methodExpression = elFactory.createMethodExpressionelContext, expression, returnType, argTypes; return methodExpression.invokeelContext, argValues; } Example 2–20 shows how you can set a new object on a managed bean. Example 2–20 Setting a New Object on a Managed Bean public static void setObjectString expression, Object newValue { FacesContext facesContext = getFacesContext; Application app = facesContext.getApplication; ExpressionFactory elFactory = app.getExpressionFactory; ELContext elContext = facesContext.getELContext; ValueExpression valueExp = elFactory.createValueExpressionelContext, expression, Object.class; Check that the input newValue can be cast to the property type expected by the managed bean. Rely on Auto-Unboxing if the managed Bean expects a primitive Class bindClass = valueExp.getTypeelContext; if bindClass.isPrimitive || bindClass.isInstancenewValue { valueExp.setValueelContext, newValue; } }

2.6 Creating and Using Managed Beans

Managed beans are Java classes that you register with the application using various configuration files. When the JSF application starts up, it parses these configuration files and the beans are made available and can be referenced in an EL expression, allowing access to the beans’ properties and methods. Whenever a managed bean is referenced for the first time and it does not already exist, the Managed Bean Creation Facility instantiates the bean by calling the default constructor method on the bean. If any properties are also declared, they are populated with the declared default values. Often, managed beans handle events or some manipulation of data that is best handled at the front end. For a more complete description of how managed beans are used in a standard JSF application, see the Java EE 5 tutorial at http:www.oracle.comtechnetworkjavaindex.html . 2-30 Web User Interface Developers Guide for Oracle Application Development Framework In a standard JSF application, managed beans are registered in the faces-config.xml configuration file.

2.6.1 How to Create a Managed Bean in JDeveloper

You can create a managed bean and register it with the JSF application at the same time using the overview editor for the faces-config.xml file. To create and register a managed bean: 1. In the Application Navigator, open the faces-config.xml file.

2. In the editor window, click the Overview tab.

3. In the overview editor, click the Managed Beans tab.

Figure 2–16 shows the editor for the faces-config.xml file used by the ADF Faces demo that contains the File Explorer application. Figure 2–16 Managed Beans in the faces-config.xml File

4. Click the Add icon to add a row to the Managed Bean table.

5. In the Create Managed Bean dialog, enter values. Click Help for more information

about using the dialog. Select the Generate Class If It Does Not Exist option if you want JDeveloper to create the class file for you. Best Practice: Use managed beans to store only bookkeeping information, for example the current user. All application data and processing should be handled by logic in the business layer of the application. Note: If you plan on using ADF Model data binding and ADF Controller, then instead of registering managed beans in the faces-config.xml file, you may need to register them within ADF task flows. For more information, refer to the Using a Managed Bean in a Fusion Web Application section of the Oracle Fusion Middleware Fusion Developers Guide for Oracle Application Development Framework.