Faces Component Binding Support in JDeveloper 10.1.3 Visual Editor
Since JavaOne is around the corner, I thought I would blog about the Faces Component Binding and Backing Bean support in the upcoming JDeveloper 10.1.3 production release.
What is Backing Bean
Backing bean is a Java Beans file/class, which could contain the properties and methods which are associated with the Faces UI Components on a page (ex: a JSP Page) <h:inputText value="#{Person.firstName}" validator="#{Person.validatename}" /> </h:inputText>
In the above example, the inputText Faces UI Component has two attributes defined in the page. The attribute 'value' refers to the 'firstName' property defined in the java file Person.java. This is called value binding, whereby you bind the UI Component's attribute to a backing bean's property. The attribute 'validator' refers to the 'validatename' method defined in the java file 'Person'. This is called method binding, whereby you bind the UI Component's attribute to a backing bean's method.
So, the backing bean can have both properties and methods referenced from Faces components in the JSP page. The backing bean is created and stored with the application using the managed bean creation facility. Once you create a backing bean, you need to add the backing bean to the application configuration file.
Now, let's see what support JDeveloper provides in creating and managing the backing bean.
Associating Backing Bean with a JSP Page
Let's see how you can associate a backing bean with a JSP page. To create a JSF-JSP Page, select File->New and bring up the New Gallery wizard. From Client Tier categories section, select JSF. On the right side, you should see all the items that can be created for JSF category. Select JSF-JSP and click 'ok'.
Note: JSF-JSP is a predefined JSP page with some faces tags like view and form in it. JDeveloper adds the f:view and h:form tags, so user does not have to manually add them.
When you click 'ok', the JSF-JSP wizard comes up. If you go through the wizard, one of the pages you should encounter is the Component Binding page. The picture below shows the Component Binding page

JDeveloper uses the terminology Auto Bind to define the features related to component binding and creation & navigation to the backing bean. The user could specify the name of the java file that should be used as backing bean. JDeveloper will create this file and add it as a managed bean in the application configuration file. If you don't specify a backing bean class name, JDeveloper will create one for you and will automatically add the backing bean as a managed bean in the application configuration file.
When you finish the JSf-JSP wizard, JDeveloper creates the jsp file and the backing file specified in the component binding page of the wizard.
Take a look at the faces-config.xml file and you will see the following entry in it
<managed-bean> <managed-bean-name>backing_untitled2</managed-bean-name> <managed-bean-class>mypackage.backing.Untitled2</managed-bean-class> <managed-bean-scope>request</managed-bean-scope> </managed-bean>
What if you don't want to specify the backing bean when you create a JSP page, but want to specify it after the page is created. JDeveloper provides a way to associate backing bean to a JSP page after the page has been created. To do this, select Design->Page Properties ... menu option. This will bring up the Page Properties dialog. One of the tabs is for 'Component Binding'. You can specify or change the backing bean file through this dialog.

How is it used
Once the backing bean is associated with a JSP page, JDevleoper maintains the relationship between the JSP page and the backing bean file. JDeveloper also provides an easy way to navigate to the backing bean from the visual editor.
Let's see the auto binding in action through a simple example.
In the visual editor, drag and drop a commandButton from the component palette (commandButton is under JSF-HTML palette page). This is how the page looks like after dropping the commandButton

Let's double click on the command button and see what happens! JDeveloper takes you to the backing bean and puts the cursor in the method commandButton_action().
Here is what happened when you double clicked on the command button in the the Visual Editor.
- a variable and accessor methods (get/set) for the command button component got added to the backing bean. - commandButon_action() method is added - this method is automatically bound to the command button component in the JSP page. If you look at the source of the JSP page, you will see the following
<h:commandButton value="commandButton1" action="#{backing_untitled2.commandButton_action}"> The action attribute is now bound (method binding) to the backing bean's method.
Now you can add logic to the commandButton_action() method that will be executed when the user clicks on the command button.
Now, let's go back to the visual editor and add an inputText component to the page.
Let's double click on the input text component and see what happens!
<h:inputText validator="#{backing_untitled2.inputText_validator}">
Here, JDeveloper added a validator method for the inputText and bound the validator attribute to the method in the backing bean.
As you have noticed, in the case of commandButton the action attribute got bound to the backing bean method and in the case of inputText, the validator attribute got bound to the backing bean method. JDeveloper will bind the action attribute if it is supported on the Faces component, if not it will bind the validator attribute if it is supported on the component.
Note: what happens if neither the action nor the validator attribute is supported by the Faces component. Go ahead and try double click on a graphicImage component and see what happens!.
Component Binding Toolbar
I am not sure what the official name for this toolbar. I am going to go with the component binding toolbar. If you take a close look at the top of the source editor, you will see a toolbar like this
There are two items in the toolbar, one for Components and one for Events. Go ahead and select the components drop down list.
It shows all the Faces UI components that are available in the JSP page. Select the commandButton1 entry and the Events entry gets enabled.
Select the action entry in the Events drop down list and JDeveloper will put the focus on the commandButton_action() method. If you have lots of faces components, this toolbar provides an easy way to navigate to the appropriate method. First select the component from the Components drop down list and then select an entry from the Events drop down list, JDeveloper will move the focus to the appropriate method.
In the above case, you also see an entry named actionListener. Selecting that will create an action listener method for the commandButton1 component and associate the method to the actionListener attribute of the commandButton1 component.
<h:commandButton value="commandButton1" action="#{backing_untitled2.commandButton_action}" actionListener="#{backing_untitled2.commandButton_actionListener}"/>
The component binding toolbar can be used to navigate to the method and can also be used to create action or event or validator methods. This toolbar provides a powerful feature to quickly create methods which can serve as the entry point to the business logic for the faces components.
Hope you will find the component binding feature useful for your web application development. This feature will be available in the production release of JDeveloper 10.1.3.
11:07:42 AM
|