How to Create an EL Expression

2-28 Web User Interface Developers Guide for Oracle Application Development Framework Selecting an item in the tree causes it to be moved to the Expression box within an EL expression. You can also type the expression directly in the Expression box. ■ Use the operator buttons to add logical or mathematical operators to the expression. Figure 2–15 shows the Expression Builder dialog being used to create an expression that binds to the value of a label for a component to the label property of the explorer managed bean. Figure 2–15 The Expression Builder Dialog

2.5.2 How to Use EL Expressions Within Managed Beans

While JDeveloper creates many needed EL expressions for you, and you can use the Expression Builder to create those not built for you, there may be times when you need to access, set, or invoke EL expressions within a managed bean. Example 2–18 shows how you can get a reference to an EL expression and return or create the matching object. Example 2–18 Resolving an EL Expression from a Managed Bean public static Object resolveExpressionString expression { FacesContext facesContext = getFacesContext; Application app = facesContext.getApplication; ExpressionFactory elFactory = app.getExpressionFactory; ELContext elContext = facesContext.getELContext; ValueExpression valueExp = Tip: For more information about these objects, see the ADF Faces Javadoc. 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 .