< Day Day Up > |
As we introduced in Chapter 2, WebLogic Workshop creates J2EE enterprise applications containing one or more projects, libraries, modules, and security roles. Before Workshop, just laying out properties and configuring an application required a considerable amount of effort. However, Workshop makes creating new Web applications quick and easy. To create a basic Workshop application, select File, New, Application from the WebLogic Workshop menu. Figure 3.8 shows the New Application dialog box. In this figure, (A) indicates the types of applications displayed in the right pane; by default, all application types are shown. The application type list, labeled as (B), indicates the type of application being created. Default application s are basic Web applications and include XML Schema support. A default application contains a core , but minimal, application as a development starting point. Empty applications include Web application development support, but without a starting application. Portal applications are default applications that include additional portal support services, including a data project supporting rules, campaigns , and other portal features. Portal applications require a Portal-enabled server. Process application s are default applications that include integration and business process services and require an Integration-enabled server. Figure 3.8. The New Application dialog box.
The directory path , labeled (C), represents the root directory where the application will be created. The new application name, labeled (D), represents the name of the application as well as the topmost directory within the root directory. The Server text box, labeled (E), represents the target server for the application. The Server text box is the most important field in the New Application dialog box. In the WebLogic Workshop programming world, applications and servers are tightly coupled . This tight coupling is a result of applications requiring specific services. Various servers provide different services and different feature sets. For example, an application might require WebLogic Portal or Integration support. Out of the box, WebLogic Workshop contains server definitions for the following:
When building an application for the first time, you must choose a server that provides the required services. If you intend to create a Web application, select the Workshop server; otherwise , choose a server consistent with your development needs. Chapter 15, "Taking Your Applications from Development to Production," discusses the WebLogic Configuration Wizard. This wizard enables users to create domain definitions that can include any combination of the services required to support different application types. On disk, Workshop applications are managed as expanded enterprise applications, but can be packaged into Enterprise Archives (EARs) easily by selecting Build, Build EAR from the main menu. The build process is examined further in "Building Applications," later in this chapter. After creating a default Web application, WebLogic Workshop displays the Application pane (see Figure 3.9). Figure 3.9. A Workshop application with Schema and basic Web application support.
Workshop Applications BreakdownWorkshop Web applications are divided into three major sections. One major section is the Web application itself, named yourname Web by default, which contains
Another major section is the default Schema project, which contains the following:
The last major section is supporting libraries and security definitions common to the application folders:
Workshop ProjectsAs we saw in Chapter 2, workshop applications contain one or more projects. These are the most common project types:
In addition, if you have purchased or downloaded WebLogic Workshop Platform Edition, you can create other project types, such as
To create a new project, select File, New, Project from the Workshop menu. Alternatively, select your application in the Application tab, right-click, and choose New Project. In the New Project dialog box (see Figure 3.10), select the type of project you would like to add. The type of project list , labeled (D) in Figure 3.10, displays subsets of the project types in the pane labeled (E). The All option displays all project types, and the other options display subsets of project types. Select the type, labeled (D), and the subtype, labeled (E), and enter your new project name in the space labeled (F). A new project is then added to Workshop, and corresponding entries are added in the underlying XML deployment descriptors. Figure 3.10. The New Project dialog box.
With all the Workshop project types, the question is "When do you use one project type versus another?" When you need to build a portal project, the answer is clear. However, as a rule of thumb, you should use a different project whenever you are creating reuseable units of functionality or to separate unrelated functional areas. As we examine controls, portals, integration and other project and application types we will also examine when it is appropriate to create a new project or application, and when to extend the current application.
Accessing and Exposing Data via ControlsApplications, whether they are designed to support Web services or be accessed as Web or portal applications, require data as content and for decision logic. In Workshop applications, logic and data are provided by controls, which are the heart of any WebLogic Workshop application. Figure 3.2 introduced the Workshop controls model; Figure 3.11 shows a deeper view. Remember, workshop supports two different types of controls: resource or extensible controls ( based on J2EE resources), resources, and custom controls written by developers. Custom controls are normally used to encapsulate data access or add business logic. To the developer, controls look much like standard Java objects; however, in addition to traditional methods , controls provide a number of other capabilities, such as
Figure 3.11. A deeper look at controls.
One area that's becoming more important in messaging is connectivity. Three specific types of connected behavior can occur in today's networks: fully connected , in which the client and server are connected all the time; disconnected , in which the client is never connected to a server; and partially connected , in which the client is sometimes connected to the server and sometimes not. The control's runtime engine has support specifically targeted toward connectivity, namely the capability to manage a conversation. Controls can clearly define what methods start, continue, and end a conversation. More important, controls provide mechanisms for obtaining conversation IDs so that applications can disconnect and then later reestablish a conversation. The ability to reestablish a conversation is key to supporting a partially connected network model. Conversations, asynchronous methods, and callbacks are covered in detail in Chapters 7, "Using the NetUI Tag Library Toolbox to Develop Sophisticated JSPs," 9, "Working with Web Services,"and 11, "Working with Workflows." Adding Controls to Page FlowsThe two most common uses of controls are to gather data for display and to make a logic decision. To use a control, you must add it to a Page Flow, open an action, and call methods on the control. Figure 3.12 shows two methods of adding a control to a Page Flow. If the control is part of the same application, it can be added by dragging the associated .jcx (Java extensible control) or .jcs (Java custom control) file into the right side of a Page Flow's Action View. Otherwise, you can right-click in the right side of a Page Flow Action View, select Controls, and then add the control. In either case, Workshop then prompts for the variable name of the new control and adds the control to your application. Listing 3.3 shows the code annotations added to a Controller when a control is added. Controls can then be accessed in actions like regular Java classes. Listing 3.3. Annotations Marking a Variable As a Controlpublic class Controller extends PageFlowController { /** * @common:control */ private controls.Weather weather; /** * @common:control */ private db.games gamesDBCtrl; Figure 3.12. Adding a control to a Page Flow.
Accessing Databases with RowsetsOne of the most common types of controls is the rowset control. The GamesDBCtrl shown in the previous example is a simple RowSet control enhanced to return value objects. Rowset controls are based on the java.sql.RowSet interface and provide a generic mechanism for accessing database data over a network. Rowsets are derived from the java.sql.ResultSet class and inherit much of the parent behavior. Rowsets can be used to add data, query for data, traverse rows of data, and delete data. The RowSet Wizard generates several methods; the following two methods are used to read data:
Rowset data is accessed by using one of the type-specific rowset methods, a generic example of which is shown here. All rowset accessor methods use the following form: Type get Type (String ColName); In this form, type can be String , Int , Double , Float , Short , and so on. See the java.sql.RowSet documentation for a complete list. Rowsets can return multiple rows of data. After a select clause, rowsets are initially positioned before the first row. You can iterate over a rowset by using the next () method. The Wonderland Casino is actually made up of a number of casinos. Descriptive data about the casinos, games, hotels, and so on is stored in a database that could be accessed by using RowSet objects. The WONDERLAND.CASINOS table contains the complete list. Its SQL definition is shown here: CREATE TABLE "WONDERLAND"."CASINOS" ("ID" INT IDENTITY, "NAME" VARCHAR(20), "DESCR" VARCHAR(512)); Assuming you have a RowSet object that contains all the casinos, you could traverse the list using the following: While (allData.next()) { System.out.println("ID=" + allData.getInt("ID"); System.out.println("Name=" + allData.getString("NAME"); System.out.println("Desc=" + allData.getString("DESCR"); } The use of NetUI tags to present data is covered in detail in Chapter 7. However, you could easily display the entire list of RowSet data using a netui-data:repeater tag in a JSP. Assuming you returned a rowset of data from the Casinos table defined in a Page Flow as public RowSet casinos , you could access the data as a table using the code shown in Listing 3.4, with important lines shaded.
Listing 3.4. The netui-data:repeater Tag<netui-data:repeater dataSource="{pageFlow.casinos}"> <netui-data:repeaterHeader> <table border="1"> <tr> <td><b>ID</b></td> <td><b>Name</b></td> <td><b>Description</b></td> </tr> </netui-data:repeaterHeader> <netui-data:repeaterItem> <tr> <td> <netui:label value="{container.item.ID}" /> </td> <td> <netui:label value="{container.item.NAME}" /> </td> <td> <netui:label value="{container.item.DESCR}" /> </td> </tr> </netui-data:repeaterItem> <netui-data:repeaterFooter> </table> </netui-data:repeaterFooter> </netui-data:repeater> Adding and Deleting with RowsetsIn additional to reading data, rowsets support deleting, inserting, and updating data. The Casino Maintenance department has requested adding new functionality to the Wonderland Web site to support listing, adding, and deleting casinos. A new Page Flow, CasinoMaintenance , has been added to the Wonderland application. It supports listing, adding, and deleting casino table entries. To add an entry to a rowset, do the following:
Listing 3.5 shows an example of these steps. Listing 3.5. The doAdd Action of CasinoMaintenanceController.jspprotected Forward doAdd(AddForm form) { String name = form.name; String description = form.description; try { RowSet newCasino = casinosdb.detailsCasinosTemplate(); newCasino.updateString("NAME",name); newCasino.updateString("DESCR",description); newCasino.insertRow(); // add the row into the rowset casinosdb.insertCasinos(newCasino); // add the row into the db } catch(SQLException sqle) { System.out.println("Unexpected SQLException on add:" +sqle); } return new Forward("success"); } Deleting a row from a database follows a similar pattern. To delete a row, follow these steps:
Listing 3.6 shows an example of these steps. Listing 3.6. The doDelete action of CasinoMaintenanceController.jspprotected Forward doDelete(DeleteForm form) { String id = form.ID; try { RowSet row = casinosdb.detailsCasinos(new Integer(id)); row.absolute(1); row.deleteRow(); casinosdb.deleteCasinos(row); } catch (SQLException sqle) { System.out.println("Unexpected SQLException on delete:" +sqle); } return new Forward("success"); } Creating Simple Rowset ControlsRowset controls are the heart of any database application. There are, in fact, two types of database access controls. Database controls, which are similar to rowset controls, require all data access to be coded by hand. Rowset controls go a step further than database controls, in that they provide a core set of access methods, based on java.sql.RowSets , for accessing data. Developers are free to add functionality, but a set of core methods are supplied. To create a rowset control, select a folder where the control will be created and follow these steps:
When you're finished, a new rowset control is created in the folder you selected. The new control can be used to access the table from a Page Flow or another control. |
< Day Day Up > |