WebSphere Studio Application Developer Version 5 Programming Guide part 28 pptx

10 217 0
WebSphere Studio Application Developer Version 5 Programming Guide part 28 pptx

Đang tải... (xem toàn văn)

Thông tin tài liệu

244 WebSphere Studio Application Developer Version 5 Programming Guide ("getCustomer_java_lang_String__customerID"))); itso.bank.model.Account[] methodResult1 = listAccountsBean.getAccounts (new java.lang.String(request.getParameter ("getCustomer_java_lang_String__customerID"))); Tailoring the generated files You could make the ListAccountsInputForm.html file look like the original index.html file. Rename Note that if you rename the files, you have to configure the initialization parameters of the controller servlet (ListAccountsController) in the web.xml deployment descriptor. Running the generated application Select the ListAccountsInputForm.html file and Run on Server (context). The application is published, the server is started, and the input page is displayed. Enter a customer number (102) and click Submit . The name of the customer and the accounts are displayed. A sample run is shown in Figure 7-61. Figure 7-61 Sample JavaBean application run Chapter 7. Developing Web applications 245 Summary In this chapter we learned about Application Developer’s facilities for creating static and dynamic Web applications. We created a Web project and imported existing resources into it. We then created static HTML pages, a cascading style sheet, servlets, JSPs, filters, and life-cycle listeners to complete the sample RedBank application, based on the MVC architectural pattern. We also used the JavaBean Web Pages Wizard to generate a Web application from a JavaBean. 246 WebSphere Studio Application Developer Version 5 Programming Guide © Copyright IBM Corp. 2003. All rights reserved. 247 Chapter 8. Developing Web applications with database access We have seen how to connect to databases and how to access them from a Java application. Application Developer contains wizards to allow you to easily access databases from a Web application. We now examine how to use those wizards, and how to test the resulting application. In this chapter, we go over the following topics:  Accessing databases from a Web application  Defining a data source in the server  Testing the database application  Accessing a database using DB Beans 8 248 WebSphere Studio Application Developer Version 5 Programming Guide Accessing databases from a Web application There are a number of ways that you can access databases from a Web application. You can write your own Java classes and access the database via standard JDBC calls. Alternatively, Application Developer supplies a library of database access beans called DB Beans. These can be used in a JSP through the <useBean> tag and can also be accessed through a set of JSP tags supplied in a tag library. An application using DB Beans or tags can be generated for you by Application Developer using a wizard based on an SQL statement. Which of these methods you choose depends on the nature of your application and the complexity of your database access. From the perspective of separating the layers of your application, using separate JavaBeans for the database access may be more appropriate because you are not mixing presentation and database logic in the JSP as you do when using DB Beans or JSP tags. In the following sections we discuss the wizard that can be used to create a view bean or a taglib application starting from an SQL statement. We then look a bit more closely at the DB Beans classes and JSP tags. Creating a Web project In this section, we use a new ItsoProGuideDataBaseWeb project:  Create a Web project named ItsoProGuideDataBaseWeb by following the instructions in “Creating a Web project” on page 185.  Use the existing ItsoProGuide enterprise application project and the default context root.  On the module dependencies page, select the ItsoProGuideJava.jar file and click Finish .  When prompted to repair the server configuration because a new project was added to the EAR file, click OK (Figure 8-1). Figure 8-1 Repair server configuration Chapter 8. Developing Web applications with database access 249 Generate Web pages from SQL queries Application Developer provides a wizard to help you create a set of Web pages and supporting Java classes starting from an existing or new SQL query. The wizard generates the required HTML pages, JSPs, and Java classes to quickly create a working skeleton application without you having to write any code. You can then expand and modify the generated code to create the finished application. To start the wizard, click File -> New -> Other . In the New wizard, select Web in the left pane and Database Web Pages in the right pane, then click Next . The first page of the wizard is displayed (Figure 8-2). Figure 8-2 Create Database Web Pages wizard: select model The following fields are available on this page:  Destination folder—Folder where the generated Web pages are stored.  Java package—Package where Java classes are generated.  SQL Statement Type—The type of action to perform on the database. 250 WebSphere Studio Application Developer Version 5 Programming Guide  Model—There are two types of models that you can use: IBM Database Access Tag Library: Two choices of tag library models are available: Select Statement and Master Details Pattern . – The Select Statement generation model creates an input form and a result table based on the given SQL query – The Master Details Pattern produces a master results table as well as a details page that displays more information on a selected row of the master results table. Both of these use JSP tag libraries to access the data. JSP tags are discussed in more detail in “Accessing a database using JSP taglib” on page 262. IBM Database Access Java Beans - Master Details Pattern: This model generates Web pages that access database information using IBM Data Access Beans to separate the database interaction from the JSP page. For this example we use this model. Clicking Next brings up the second wizard page (Figure 8-3). Figure 8-3 Create Database Web Pages wizard: select SQL statement You have two choices: use an existing SQL statement, or build a new one:  If you already have an existing SQL statement in the statement folder of a database project, you can select it here and use that to generate the Web pages. Chapter 8. Developing Web applications with database access 251  If you deselect Use Existing SQL Statement and click Next , you are prompted to build an SQL statement in the same way as described in “Creating SQL statements” on page 165. We select the ListCredits SQL statement that we built in “Using SQL Query Builder” on page 171 and click Next . Clicking Next displays the page where you decide how you want to access the database when the generated application is run (Figure 8-4). You can choose to use a direct connection or use a data source. For a discussion about these two different ways of connecting, see “Data source versus direct connection” on page 141. Figure 8-4 Create Database Web Pages wizard: connection information In this case we use the data source connection. We specify the JNDI name of the data source, which in our case is jdbc/ejbbank. On the next three wizard pages you can view and change the pages that will be generated by the wizard. The first page is the View Bean Data Page, where you can specify the style sheet, error page, whether to store results in the session or request object, and whether or not to create a Front Controller, use an existing one, or not use one at all (Figure 8-5). 252 WebSphere Studio Application Developer Version 5 Programming Guide Figure 8-5 Create Database Web Pages wizard: View Bean Data Page The Store results option determines where the results from the query should be stored. You can choose to store them in the session, in which case they will be available to other pages for the life of the session, or in the request. You should be aware of potential memory issues if you choose to store a large result set in the session. The front controller option decides if a controller servlet is generated or reused. The controller servlet uses initialization parameters that specify for each action which JSP to call. On the following page, you see the HTML input form (Figure 8-6). Chapter 8. Developing Web applications with database access 253 Figure 8-6 Create Database Web Pages wizard: design input form Here you can make some changes to page and field properties. Notice that the two input fields are automatically generated. This is where the :type and :lastname host variable values will come from. Select each host variable and change the label to the desired text, for example, Transaction type and Last name. Once this and the following pages have been generated, you can make further changes using Page Designer. The next page shows the master result page. The default is to use a table to display the result rows from the query (Figure 8-7). Last name . use an existing one, or not use one at all (Figure 8 -5) . 252 WebSphere Studio Application Developer Version 5 Programming Guide Figure 8 -5 Create Database Web Pages wizard: View Bean Data Page The. Statement Type—The type of action to perform on the database. 250 WebSphere Studio Application Developer Version 5 Programming Guide  Model—There are two types of models that you can use: IBM. a Web application  Defining a data source in the server  Testing the database application  Accessing a database using DB Beans 8 248 WebSphere Studio Application Developer Version 5 Programming

Ngày đăng: 03/07/2014, 20:20

Tài liệu cùng người dùng

Tài liệu liên quan