Creating a BSP using the Model View Controller ( MVC ) technique
Tutorial 2 - Creating the Model ( Class to to perform functionality i.e. retrieve data )
Step 1 - Using the Model class within DO_INIT (note: Model class not created yet!)
From within SE80 double click on main.do to select it, now double click on the controller class.
Double click on the DO_INIT method. Now enter the following code into the DO_INIT method & save.
method DO_INIT.
*CALL METHOD SUPER->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.
endmethod.
Step 2 – Create model class
Use SE80 or SE18 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 3 – 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
UP TO 1 ROWS
INTO retvalue
FROM ekko.
ENDSELECT.
ENDMETHOD.
Step 4 – 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 5 – Define atributes of the controller sub class
Now return to the controller class you created, accessed via the controller page (i.e. main.do). Remember
the ABAP code you inserted to declare 'r_model' within the DO_INIT method? You now need to declare this
attribute within the class attributes tab. It needs to be instance, public and 'type ref to' your model
class ( ZCL_MODEL_01 ). Save and Activate the controller class ( ZCL_CONTROLLER_01 ).
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:form>
<htmlb:textView text = "Purchase order"
design = "EMPHASIZED" />
<htmlb:inputField id = "<%=p_ord->retvalue%>"
invalid = "false"
value = "test"
required = "true"/><BR>
<htmlb:button text = "Press Me"
onClick = "myClickHandler" />
</htmlb:form>
</htmlb:page>
</htmlb:content>
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 .
*CALL METHOD SUPER->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 ).
ENDMETHOD.
Tutorial 1 Tutorial 2 Tutorial 3
|