This is Part 3 of a series of articles exposing the features of the Java™ Server Faces Technology Preview within WebSphere® Studio V5.1.1. The articles in this series include:
- Part 1: Creating a JSF Web project and page template
- Part 2: Creating JSF submission forms
- Part 3: Creating JSF applications that access data using Web Data Objects (WDO, soon to be SDO: Service Data Objects)
- Part 4: Integrating JSF applications with Enterprise Java Beans using action handlers and JavaBean data components
- Part 5: Creating a JSF Web service client using the Web service proxy component.
In Part 2, we saw how to build JSF forms using Rapid Application Development (RAD) Java Server Faces (JSF) components. In Part 3, we will see how JSF can work with data using Web Data Object (WDO) technology. WDO is the IBM precursor to the emerging Service Data Object (SDO) standard currently in development within the Sun Java Community Process as JSR 235. JSF and SDO make a powerful combination that enables Web Applications to interact with enterprise data in a visual way, and does so without compromising good design.
You will need to download the materials includes with this article to follow along and complete the exercise. The solution to the exercise from Part 2 is included in the download file, in case you have not completed it. You can import the EAR file as illustrated in Part 2.
To run the example, we first need to create a database. We use DB2® here, but the database scripts are included in case you want to use it as a model for a different database. After creating the database, we will use WebSphere Studio to create a WDO component and drag it onto to a display page. We will test the application using the built-in unit test environment (UTE), and then lastly, you will see how JSF has a built in validation framework by applying validation to the Stock History Form.
You need to create a database before starting the exercise. To create a DB2 database:
- Download the materials and database scripts provided in the Download section of this article, if you have not already done so.
- Open a DB2 Command Window. This can be done through the Windows® Explorer (Figure 1).
Figure 1. Open DB2 command window
- Switch to the directory where you extracted the ZIP file to and go to
<Extracted Dir>\JSFArticleSeries\Part3\DBScript. - Type the command
CreateWSTrade(Figure 2). This will create the necessary database tables. (We usedb2admin/db2adminfor the userID/password; if you wish to change this, you can edit theCreateWSTrade.batfile.)
Figure 2. Create database tables
- To verify that the database is installed correctly, connect to the database from the DB2 command window by typing (Figure 3):
db2 connect to WSTRADE user db2admin using db2admin
Figure 3. Connect to the database
- Type the command:
db2 select * from WSTRADE.STOCKHISTORY. You should get some results back, similar to Figure 4.
Figure 4. Database inquiry
- Close the DB2 command window.
Create a JSP using JSF/WDO components
We are now ready to create our first JSF form. Your WebSphere Studio workspace should be open from the last lab.
- First, we are going to create a new JSF page that will display the results of submitting a Stock History Request.
- From the Web perspective, expand the PersonalTradeJSF Web project, right-click the WebContent folder, and select New => Faces JSP File (Figure 5).
Figure 5. Project Navigator
- Name the new JSP file
viewStockHistory.jsp, check Create from page template, and press Next. (Figure 6)
Figure 6. Create a Faces JSP file
- On the next dialog, make sure the StockPageTemplate.jtpl is selected and then Finish.
Figure 7. Create a Faces JSP file
- From the Web perspective, expand the PersonalTradeJSF Web project, right-click the WebContent folder, and select New => Faces JSP File (Figure 5).
- The accessStockHistory page stores the symbol value as input in the request scope. Since accessStockHistory will forward to the viewStockHistory page, the latter page will have access to the input to pass the symbol to the WDO object.
- Ensure that viewStockHistory is the active JSP in the editor. In the Page Data view, add a new requestScope variable (Figure 8).
Figure 8. Page Data
- The variable name is
symboland the type isjava.lang.String(Figure 9). This should be the same information as on the submitting page. Select OK.
Figure 9. Add Request Scope Variable
Figure 10. requestScope result
- Ensure that viewStockHistory is the active JSP in the editor. In the Page Data view, add a new requestScope variable (Figure 8).
- Next, we will build the result page using a WDO Data Component. WebSphere Studio automatically connects to the database and builds the necessary data components.
- First, we need to create a title label. From the JSF palette, select an Output component (Figure 11).
Figure 11. JSF palette
- Drag the coomponent on the JSP page, between the start and end markers, and select Text as the Type.
Figure 12. JSF palette
- Change the font size of the text to 18 in the Attribute view.
Figure 13. Attribute view
- Create a blank line after the output component (Figure 13). Expand the Data (Beta) section of the palette. Select Relational Data List (Figure 14) and drag it onto to the JSP page, as shown in Figure 15.
Figure 14. Visual palette
Figure 15. Stock History page
- The Data wizard will display (Figure 16). Enter or select the following values:
- Name:
stockHistory - Scope: request
- Action Type: Display existing data list
Figure 16. Data wizard
- Name:
- In the New Connection dialog, enter
WSTRADEfor Connection name, then select New Connection. Press Next.
Figure 17. Connect to a database
- In the Database Connection dialog (Figure 18), enter or select the following values:
- Connection Name :
WSTRADE - Database: WSTRADE
- User ID: db2admin
- Password:
db2admin - Database vendor type: DB2 Universal Database V8.1
- JDBC Driver: IBM DB2 APP DRIVER
- Class location: should already contain the proper path.
Figure 18. Establish a JDBC connection
- Connection Name :
- In the Add Data List dialog (Figure 19), select the table WSTRADE.STOCKHISTORY and select Next.
Figure 19. Data List Properties
- In the next dialog (Figure 20), leave all the fields checked and select Next.
Figure 20. Column selection and advanced tasks
- On the next dialog (Figure 21), select the + button to add a condition.
Figure 21. Add conditions and orders
- For Condition, select By columns, for Column, select SYMBOL and =. Next to the Value section, press the ... button.
Figure 22. Conditions
- Select the symbol variable from requestScope (Figure 23), then OK.
Figure 23. Select Page Data Object
- This condition allows the data to be retrieved by input of the symbol on the request scope. Press OK (Figure 24) then Finish (Figure 25).
Figure 24. Conditions
Figure 25. Add Conditions and Orders
- The JSP page should now contain the data grid component based on the table, as shown in Figure 26.
Figure 26. JSP page with data grid
- The Page Data will have the stockHistory object defined (Figure 27).
Figure 27. stockHistory object defined
- If you open the
faces-config.xml file, you will notice that the data component is defined as a managed bean. You can findfaces-config.xml filein theWEB-INF/wdodirectory. The file is listed below.<managed-bean> <managed-bean-name>stockHistory</managed-bean-name> <managed-bean-class>com.ibm.etools.webtools.wdo.rdb.internal.DataListAccessBeanImpl </managed-bean-class> <managed-bean-scope>request</managed-bean-scope> <managed-property> <property-name>connectionName</property-name> <value>WSTRADE1</value> </managed-property> <managed-property> <property-name>inputPath</property-name> <value>/WEB-INF/wdo/stockHistory.xml</value> </managed-property> <managed-property> <property-name>servletContext</property-name> <value-ref>${facesContext.externalContext.context}</value-ref> </managed-property> <managed-property> <property-name>servletRequest</property-name> <value-ref>${facesContext.externalContext.request}</value-ref> </managed-property> <managed-property> <property-name>action</property-name> <value>fill</value> </managed-property> <managed-property> <property-name>params</property-name> <map-entries> <map-entry> <key>requestScopesymbol</key> <value-ref>${requestScope.symbol}</value-ref> </map-entry> </map-entries> </managed-property> <managed-property> <property-name>targetPageSize</property-name> <value>0</value> </managed-property> </managed-bean>
- First, we need to create a title label. From the JSF palette, select an Output component (Figure 11).
- We have the data control on the page, but we need to modify the visual structure of the table to meet the requirements. We can use the Attributes View to update the table properties.
- Select one of the columns on the table, then on the Attribute View, select HX:DATAGRID. This will let us update the data grid table on the JSP page (Figure 28). (There are several useful items on the Basics tab in the Attributes view. You can add different elements, such as a table header or footer, add a row select check box, and you can also support pagination for a data list.)
Figure 28. Attribute view
- Select the Columns tab. Order the columns to display in the sequence shown in Figure 30.
Figure 30. Ordering columns
- Select the Columns tab and make the border 0.
Figure 31. Data grid attributes
- The screen should now look like Figure 32. You can preview the page as well.
Figure 32. Page preview
- Select one of the columns on the table, then on the Attribute View, select HX:DATAGRID. This will let us update the data grid table on the JSP page (Figure 28). (There are several useful items on the Basics tab in the Attributes view. You can add different elements, such as a table header or footer, add a row select check box, and you can also support pagination for a data list.)
Now that we have created our Stock History page, we now need to define the navigation rule from accessStockHistory.jsp. WebSphere Studio uses the Attribute view to do this.
- First, we will define the Navigation:
- From the Project Navigator, open accessStockHistory.jsp.
Figure 33. Project Navigator
- Select the Access Stock History button.
Figure 34. Assign navigation
- Go to the Attribute view, select the Navigation tab, then press the Add button.
Figure 35. Navigation attributes
- Select viewStockHistory.jsp from the Page dropdown box (Figure 36). This defines which page this control will forward to. For Alias, select success. Ensure that This page only is selected, then press OK. Whenever success is encountered, the faces Servlet will forward to
viewStockHistory.jsp. The success String can be returned as a result of an Action, or implicitly set as a default when submitting.
Figure 36. Add navigation rule
- The Navigation should be defined as shown in Figure 37. This will also add the navigation entry to the
faces-config.xmlfile.
Figure 37. Navigation entry
- From the Project Navigator, open accessStockHistory.jsp.
- We now need the submission to trigger the success action so that the forward will succeed. We will not need a separate action for this exercise. The request variable is bound. Also,
viewStockHistory.jspwill use a pull MVC style; only the input (the symbol variable) needs to be pushed.- With the
accessStockHistory.jspstill active in the editor, select the All tab in the Attributes view and enter success for the action.
Figure 38. Assign success action
- Save both JSF pages. Open the
faces-config.xmlif you want to examine the navigation rule, which is shown here:<navigation-rule> <from-tree-id>/accessStockHistory.jsp</from-tree-id> <navigation-case> <from-outcome>success</from-outcome> <to-tree-id>/viewStockHistory.jsp</to-tree-id> </navigation-case> </navigation-rule>
- With the
By adding the WDO data component, WebSphere Studio automatically added the data source to the WebSphere Unit Test Environment, enabling us to automatically test the application directly. To test the application:
- Right-click the accessStockHistory.jsp file in the Project Navigator, then select Run on Server.
Figure 39. Run the application
- When the server starts and the browser renders the form, enter
IBMin the blank field and press Access Stock History (Figure 40). The stock history data should display (Figure 41).
Figure 40. New JSP page
Figure 41. Successful result
You can easily apply a level of default validation from the validation framework built into JSF. We will add validation to the accessStockHistory.jsp page in the form of a required field; we will make the symbol field a required entry so that if it is left blank, an error message will display:
- Open accessStockHistory.jsp and select the symbol input control (Figure 42).
Figure 42. accessStockHistory.jsp
- In the Attributes view (Figure 43), select Value is required and Add Display Error control.
- Click again on the symbol input control on the JSP page to associate the control with the attribute (Figure 42).
- In the Attributes view, select the Set Custom Message button (Figure 43).
Figure 43. Attributes view
- In the Set custom message dialog, add the text
Symbol is requiredto the Message text box (Figure 44).
Figure 44. Set custom message
- A placeholder for the error message will appear in the JSP (Figure 45).
Figure 45. JSP with error message pplaceholder
- To test the validation, right-click on the server from the Server view, then select Restart Project => StockSystemEAR (Figure 46).
Figure 46. Restart project
- Again, right-click the accessStockHistory.jsp and select Run on Server.
- When the browser renders, press the Access Stock History button without entering any text. The validation error message should display.
Figure 48. Validation error message
- To stop the server, go to the Servers view, right-click on the server and press Stop.
Figure 49. Stop server
In this installment of our article series, we showed how JSF and WDO (soon to be SDO) make a powerful combination for building dynamic Web pages quickly and efficiently. We also demonstrated how JSF supports validation in an easy and "toolable" way.
In Part 4, we will see how JSF can work just as easily with other J2EE technologies. Specifically, we will use the JavaBean component to connect our JSF pages to existing EJB applications.
| Name | Size | Download method |
|---|---|---|
| JSFArticleSeriesP3.zip | 4.8 MB | FTP |
Information about download methods
- IBM WebSphere Developer Technical Journal
- Part 1: Creating a JSF Web project and page template
- Part 2: Creating JSF submission forms
- Part 4: Integrating JSF applications with Enterprise Java Beans using action handlers and JavaBean data components
- JavaServer Faces Sun Web site
- Part 5: Creating a JSF Web service client using the Web service proxy component
- JSF Central

Roland Barcia is a Consulting IT Specialist for IBM Software Services for WebSphere. He is a co-author of IBM WebSphere: Deployment and Advanced Configuration. For more information about Roland, see his blog.
Comments (Undergoing maintenance)





