Home > Articles > Web Services > WebSphere

  • Print
  • + Share This
This chapter is from the book

Tutorial 7.2: Create and Test a Portlet That Accesses a Database

In this tutorial, you will create a service provider model to retrieve data from the database and create a service consumer model to present the data.

Step 1: Create a Service Provider Model

In this step, you will create a service provider model.

  1. Launch the WebSphere Portlet Factory Model Wizard to create a service provider model. It is one of the sample models provided by the IBM WebSphere Portlet Factory product. This sample model allows you to execute SQL statements against a database.
    1. Right-click PetTravel and click New, WebSphere Portlet Factory Model.
    2. Select PetTravel as the project. Click Next.
    3. Under Service Providers, select Database Service Provider as shown in Figure 7.55. Click Next.
      Figure 7.55

      Figure 7.55 Select to create a Database Service Provider model

    4. Enter PetTravelServiceProvider as the service name. Click Next.
    5. In step 1 of defining the service operation as shown in Figure 7.56, select jdbc/PetDB from the drop-down for SQL datasource and enter Select * from Administrator.PetTravelCompany as the SQL statement. Click Next.
      Figure 7.56

      Figure 7.56 Step 1 of defining the service operation

    6. In step 2 of defining the service operation as shown in Figure 7.57, enter getCompany as the operation name and keep all other defaults. Click Next.
      Figure 7.57

      Figure 7.57 Step 2 of defining the service operation

    7. Enter PetTravelCompany as the model name. Click Finish.
  2. The PetTravelCompany model has been created successfully as shown in Figure 7.58. The outline window at the bottom shows the list of builders in this model.
    Figure 7.58

    Figure 7.58 Database Service Provider model created

  3. Test the model.
    1. PetTravelCompany should be the selected model at this time. To test the model, click the Run Active Model icon.
    2. Your model should run successfully. You should see the getCompany operation you defined in the browser as shown in Figure 7.59. Click getCompany.
      Figure 7.59

      Figure 7.59 Database Service Provider model run successfully

    3. You should see the data from the database table PetTravelCompany displayed correctly as shown in Figure 7.60.
      Figure 7.60

      Figure 7.60 Data from database retrieved and displayed

In this step, you created a service provider model. You should see that the pieces are starting to come together. The service provider retrieves the data in the database table created in the "Tutorial Setup" section.

Step 2: Create a Service Consumer Model

In this step, you will create a service consumer model that will use a simple table to display the results from the provider model.

  1. Launch the WebSphere Portlet Factory Model Wizard to create a service consumer model. It is one of the sample models provided by the IBM WebSphere Portlet Factory product. This sample model displays data from a database.
    1. Right-click PetTravel and click New, WebSphere Portlet Factory Model.
    2. Select PetTravel as the project. Click Next.
    3. Under Service Consumers, select List and Detail Service Consumer as shown in Figure 7.61. Click Next.
      Figure 7.61

      Figure 7.61 Select to create a List and Detail Service Consumer model

    4. Enter PetTravelCompanySimpleTable as the name and select PetTravelCompany as the provider model as shown in Figure 7.62. Recall that you created the PetTravelCompany model in the previous step. Click Next.
      Figure 7.62

      Figure 7.62 Enter name and service

    5. Select getCompany from the drop-down for view data operation to provide the view data as shown in Figure 7.63. Recall that you defined the service operation getCompany to retrieve data from the database table PetTravelCompany. Click Next.
      Figure 7.63

      Figure 7.63 Specify service operation to provide view data

    6. Select COMPANY from the drop-down for details link column and keep all other defaults as shown in Figure 7.64. Recall that you created the database table PetTravelCompany with ID, Company, and Desc as the three columns. This is why Company appears as a choice in the drop-down. Click Next.
      Figure 7.64

      Figure 7.64 Specify how to get detail data

    7. Enter ListPetTravelCompany as the model name. Click Finish.
  2. The ListPetTravelCompany model has been created successfully as shown in Figure 7.65. The outline window at the bottom shows the list of builders in this model.
    Figure 7.65

    Figure 7.65 List and Detail Service Consumer model created

  3. Test the model.
    1. ListPetTravelCompany should be the selected model at this time. To test the model, click the Run Active Model icon.
    2. You should see the data from the database table PetTravelCompany displayed correctly in a simple table with the Details link using the Company column as shown in Figure 7.66.
      Figure 7.66

      Figure 7.66 List and Detail Service Consumer model run successfully

  4. Build a portlet WAR file for deploying in production. IBM WebSphere Portal is the production server.
    1. Right-click PetTravel and click Portal Server WAR, Build Portlet WAR for Production Deployment as shown in Figure 7.67.
      Figure 7.67

      Figure 7.67 Build portlet WAR for production

    2. Keep the defaults as shown in Figure 7.68. Click Finish.
      Figure 7.68

      Figure 7.68 Select portlet API and build path

    3. Go to the above build path. You should see the PA_PetTravel.war WAR file.

You have created a service consumer model to present data in a simple table.

  • + Share This
  • 🔖 Save To Your Account