Start of Content Area

Procedure documentation Creating a Component Context and Binding It to the Model  Locate the document in its SAP Library structure

Each Web Dynpro component is supplied with a corresponding Component Controller. This controller is responsible for the acquisition of the data required by the QuickCarRentalService Web service for car reservations. Accordingly, it must be able to depict the corresponding input and output structures of the CarRental model. In order to create a connection between the Component Controller and the model created in the previous step, you will bind the context of the Component Controller to the created model structure using the Data Modeler.

The following figure explains how the structure of the context tree generally results from the model classes and their relations to one another – on the basis of the model binding. 

This graphic is explained in the accompanying text

 

Prerequisites

This graphic is explained in the accompanying text

The structure of your project TutWD_CarRental_Init is currently displayed in the Web Dynpro Explorer.

Procedure

Adding a Model to the Web Dynpro Component

...

       1.      Open the Data Modeler through the context menu entry This graphic is explained in the accompanying text Open Data Modeler of the node CarRentalComp.

       2.      In the toolbar on the left, choose the This graphic is explained in the accompanying text Add an existing model to the component icon.

The icon will then be highlighted in gray.

       3.      Place the cursor on the This graphic is explained in the accompanying text Used Models area and left-click.

       4.      Select the CarRentalModel checkbox and confirm by pressing OK.

The structure of the Web Dynpro component CarRentalComp will look like this in the Data Modeler:

This graphic is explained in the accompanying text

Binding the Component Controller Context to the Web Service

You can declare the binding between the context of the component controller and the created CarRentalModel – in a very simple manner from the Data Modeler you just started – using a service controller template:

Ön...

       1.      In the Data Modeler, choose the icon for Component Controller and start the respective context menu.

       2.      From the context menu, choose the option Apply Template.

       3.      Choose the Service Controller template in the displayed wizard and then Next.

This graphic is explained in the accompanying text

       4.      Now choose the model class Request_QuickCarRentalServiceViDocument_saveBooking and navigate further using Next.

       5.      In the following input dialog, select all the model nodes and model attributes.

This graphic is explained in the accompanying text

In general, it is appropriate to keep the similarity of names between the model classes and parameters and the corresponding context nodes or context attributes. However, the root node for the model is derived directly from the model main class Request_QuickCarRentalServiceViDocument_saveBooking with the same name: . In this case, it is better to take a shorter name, such as SaveBooking.

       6.      Now edit the new name SaveBooking in the first line underneath the table column Name.

This graphic is explained in the accompanying text

       7.      Close the template wizard using Finish and save the project.

Result

From the generated model classes, you have now defined a structure of context elements (consisting of model nodes and model attributes) in the component controller CarRentalComp, and you have also bound these to the respective parameters of the model classes.

Also, with the help of the service controller template in the component controller, you have generated the code in the two methods wdDoInit() und executeSaveBooking( ).

Discussion

In every controller there are predefined locations where you – as application developer – can insert the source code. The standard methods wdDoInit() andwdDoExit() belong to these locations. wdDoInit() is always controlled if the controller is instanced. For this reason, you must remember that at this point there is an object which – at runtime – represents the entire input, including all the input parameters for calling the Web Service business method. In addition, you will need a further controller method in which the actual call of the Web service method is triggered. This is done in the method named execute_Save( ). Since you have implemented the template wizard, you do not need to implement these methods in the component controller yourself. The generated lines are discussed below:

Set up the access to the Web Service model within the method wdDoInit(). Here you define a request object that – at runtime – represents the input page of the Web service method. This object must also be bound to the model node SaveBooking (that was declared at Design time).

 

  public void wdDoInit()

  {

    //@@begin wdDoInit()

   //$$begin Service Controller(-1547658955)

   wdContext.nodeSaveBooking().bind(new Request_QuickCarRentalServiceViDocument_saveBooking());

   //$$end

    //@@end

  }

 

The actual Web service is now called through the execute() method call of the model object currently selected in the context model node. The data stored in the controller context is a copy of the data stored in the model – that is, the one does not directly reference the other. Therefore, the controller context does not yet contain the returned results of the Web service call executed previously and stored in the model. For this reason, the model node Response (exists as inner node underneath the SaveBooking node in the context) is explicitly invalidated. The result is that the response data currently stored in the model is transferred to the respective context node element.

 

  //@@begin javadoc:executeSaveBooking()

  /** Declared method. */

  //@@end

  public void executeSaveBooking( )

  {

    //@@begin executeSaveBooking()

   //$$begin Service Controller(-1285036589)

   IWDMessageManager manager = wdComponentAPI.getMessageManager();

   try{

      wdContext.currentSaveBookingElement().modelObject().execute();

      wdContext.nodeResponse().invalidate();

      wdContext.nodeResult().invalidate();

   } catch(Exception ce) {

      manager.reportException(ce.getMessage(), false);

   }

   //$$end

    //@@end

  }

 

Next Step:

Mapping the View Context onto the Component Context

 

 

End of Content Area