!--a11y-->
Adding Source Code for the Web Service
Connection 
After you have successfully executed all the required declarative development steps, you now need to add some individual lines of Java code in the source code of the view controller.
Within each controller, there are some predefined places where you can add source code. These include, in particular, the standard methods wdDoInit() and wdDoExit(),and also the Action Event-Handler in the view controllers.
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 the action event handler onActionRentCar(), the actual Web service call must be implemented, based on the data entered by the user and stored in the context concerned.
|
|
You have created the model based on the CarRental Web service. |
|
|
You have mapped the view context onto the Component Controller context. |
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).
...
1. Choose the Implementation tab for the FormView.
After the generation routines have been run once again, the updated source code of the view controller implementation is displayed.
2. Between //@@begin wdDoInit() and //@@end of the method wdDoInit(), enter the following Java code:
/** Hook method called to initialize controller. */ public void wdDoInit() { //@@begin wdDoInit() Request_QuickCarRentalServiceViDocument_saveBooking req = new Request_QuickCarRentalServiceViDocument_saveBooking(); wdContext.nodeSaveBooking().bind(req); //@@end } |
To enter the source code, you can avail of the Code Assist functions provided by the Developer Studio by selecting the keyboard combination CTRL+SPACE.

The actual Web service is now called through the execute() method call of the model object currently stored in the context model node. This already contains the reservation data entered by the user (through data binding and context mapping).
The data stored in the view context is a copy of the data stored in the model – that is, the one does not directly reference the other. Therefore, the view context does not yet contain the returned results of the Web service call executed previously and stored in the model.
For this reason, you therefore need to explicitly invalidate the model node response (this is contained in the context as an inner node underneath the node SaveBooking ). The response data most recently stored in the model is then transmitted to the corresponding context node element.
The returned results (in the example application, solely the reservation ID number and the rental price) are then displayed automatically in the UI through data binding.
...
1. In the onActionRentCar() method, add the following source code:
/** declared validating event handler */ public void onActionRentCar(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent ) { //@@begin onActionrRentCar(ServerEvent) try { wdContext.currentSaveBookingElement().modelObject().execute(); wdContext.nodeResponse().invalidate();
MessageManager msgMgr= (MessageManager) wdThis.wdGetAPI().getComponent().getMessageManager(); msgMgr.reportSuccess("Success!"); } catch(Exception ex) { MessageManager msgMgr = (MessageManager) wdThis.wdGetAPI().getComponent().getMessageManager(); msgMgr.reportException(ex.getLocalizedMessage(), true); } //@@end } |
2. In the context menu of the source code editor, choose the function Source ->Organize Imports in order to add the missing import line.
The MessageManager class is imported into the view controller and you can use the generic UI service to display message texts in the user interface.
... //@@begin imports import com.sap.tc.webdynpro.progmodel.controller.MessageManager; ... //@@end } ... |
The Developer Studio updates and compiles the Java classes belonging to your project. (Note: Compilation only occurs if you are using the Workbench standard settings.) After you have done this, no more error messages should appear in your tasks view.
Building, Deploying, and Running the Project
