sapdev logo background
sapdev logo sapdev logo

Creating a BSP using the Model View Controller ( MVC ) technique

This is Tutorial 2 in this mini series of BSP applications using MVC techniques and shows you how to create a model class. The model class is used to perform functionality such as processing and retrieving data. If you have not created an MVC BSP before you will need to do Tutorial 1 first which will only take a few minutes. The steps below then adds functionality to the application created in tutorial 1.

Step 1 - Create model class
Use SE80 or SE24 to create a new class, give it a name and description.

Go to the properties tab and enter change mode, Press the Superclass button and enter the superclass cl_bsp_model. Save and activate

Step 2 - Define method of Model class
Select the methods tab and scroll to the bottom of the methods, now enter a new method called SELECT_DETAILS, as an instance method with public visibility.

Now double click the method to create it and enter the following code:

METHOD select_details .
  SELECT ebeln
    INTO retvalue
    FROM ekko.

Step 3 - Define attributes of method
Click on the Model class attributes tab and enter the field 'RETVALUE' as type ekko-ebeln, Ensuring it is an instance attribute, which has public visibility. Now save and activate the new model class!

Step 4 - Define atributes of the controller sub class
From within SE80 double click on to select it, now double click on the controller class. First we need to declare an attruibute which we can then use in the DO_INIT method. This is done within the class attributes tab and needs to be instance, public and 'type ref to' your model class ( ZCL_MODEL_01 ). Press Save.

Step 5 - Using the Model class within DO_INIT
Now still within the controller class accessed via the controller page (i.e. We need to add some ABAP code to declare 'r_model' within the DO_INIT method? To do this Double click on the DO_INIT method. Now enter the following code into the DO_INIT method & save. Then Save and Activate the controller class ( ZCL_CONTROLLER_01 )

method DO_INIT.
*    .
* Create refernece variable based on your own class (not created yet)
data: r_model TYPE REF TO zcl_model_01.

* Create an instance of our Model class and use a widening cast to load your
* reference variable r_model
r_model ?= me->create_model(
       class_name = 'ZCL_MODEL_01'
       model_id   = 'mod_main' ).

* Use the r_model to call the select details method from your Model class
r_model->select_details( ).
* Load attributes in your class attributes to hold the variable - make it
* more 'global' so it can be seen by other methods.
me->r_model = r_model.

Step 6 - Display data from the model (update the page/view)
In-order to display the data from the model, we are going to use a reference variable p_ord declared in the page attributes .

Now make changes to the layout, so that the returned data is displayed within and input field.

<%@page language="abap"%>
<%@extension name="htmlb" prefix="htmlb"%>

<htmlb:content design="design2003">
  <htmlb:page title = " ">
      <htmlb:textView    	text          = "Purchase order"
                            	design        = "EMPHASIZED" />
      <htmlb:inputField   	id            = "ID1"
            		   	invalid       = "false"
                            	value         = "<%=p_ord->retvalue%>"
                            	required      = "true"/><BR>
      <htmlb:button       	text          = "Press Me"
                            	onClick       = "myClickHandler" />

Step 7 - Display data from the model (update controller)
Within the DO_REQUEST of the controller class ( ZCL_CONTROLLER_01 ) enter the code below to pass the model reference back to the View. Save and activate everything.

METHOD do_request .
*    .
  DATA: r_view TYPE REF TO if_bsp_page.
  r_view = create_view( view_name = 'main1.htm' ).
  r_view->set_attribute( name  = 'p_ord'
                         value = me->r_model ).
  call_view( r_view ).

Tutorial 1 Tutorial 2 Tutorial 3

comments powered by Disqus