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
   UP TO 1 ROWS
    INTO retvalue
    FROM ekko.
  ENDSELECT.
ENDMETHOD.


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 main.do 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. main.do). 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.
*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 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            = "ID1"
            		   	invalid       = "false"
                            	value         = "<%=p_ord->retvalue%>"
                            	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


Related Articles

Javascript to capture when a user closes or leaves sap bsp html page
Using AJAX functionality within our SAP BSP
Close BSP Session (type 'Pluggin HTTP') including when in SAP portal
SAP BSP to auto select region when user selects country using HTML and Javascript
HTML and Javascript code to force enter key to perform tab
JavaScrip command getElementById useful examples
MVC BSP input field - Demonstrate how to retrieve a value entered into text input field
Allowing multiple instances of a BSP application to be run by a user
Execute Standard SAP Transaction from BSP using dynamically created shortcut
Uplaod file within SAP BSP application using HTML and ABAP coding
UUseful SAP BSP application code - ABAP, HTML, HTMLB, Javascript, CSS
Creating a BSP Application class to help store and pass data betwenn your BSP pages
Training Course and Workshop Booking Form
BSP checkboxes using HTML and the OnInputProcessing BSP Event
BSP checkboxes using HTML and the OnInputProcessing BSP Event
Check HTML checkbox is checked using Javascript within your SAP BSP
BSP Dropdown List - create a BSP dropdown list which allows user selection!!
ABAP BSP to allow user to select from SAP HR org structure - CSS file
Java script to display tree structure on your SAP BSP web page
Create a Simple BSP - Simple BSP to display text and call section BSP page using HTML
SAP BSP Training course booking - Creating an example training course booking form using BSP
Creating an SAP BSP which executes an ABAP report to retrieve data for use within BSP application
org selection BSP - detials.htm page
Display page of the SAP BSP example application to store favourite tcodes as a cookie
Execute.htm page of the SAP BSP example application to store favourite tcodes as a cookie
Favourites cookie bsp - initial page to display input fields and retrieve user input
BSP using cookies - Demonstrate use of cookies within a BSP to store user favourits
ABAP Export data to memory - Demonstrate use of ABAP program to retrieve data for use in BSP
MVC BSP HTMLB input field - Demonstrate how to retrieve a value entered into text input field
BSP development using standard HTML code instead of Business HTML (HTMLB)
Business HTML (HTMLB) - List of business HTML tags you can use within your BSP
Adding JavaScript to BSP (HTML) pages - Shows how to add javascript to your BSP pages
Example JavaScript code which can be added to your BSP application
BSP MIME Objects for Org. search application
Call onInputProcessing event from SAP BSP page via href or button
Organisation selection BSP - close internet window using javascript
Organisation selection BSP - get server side cookie ( get_server_cookie )
orgaisation selection BSP - set and get server side cookie ( set_server_cookie )
Display orgaisation selection BSP
BSP which allows HR Organisation Structure search and selection using javascript
Organisation update program - get server side cookie ( get_server_cookie )
BSP to capture user entry into HTML input fields
Capture BSP radiobutton selection using HTML and the OnInputProcessing BSP Event
BSP checkboxes using HTML and the OnInputProcessing BSP Event
Stateful and stateless BSP applications
Get user entry into HTML input fields with SAP BSP
Javascript to capture when a user closes or leaves sap bsp html page
Creating a BSP using the Model View Controller ( MVC ) technique
Using SAP Model View Controller (MVC) development techniques to develop BSPs
Adding a JavaScript date selection field to a BSP
Creating a BSP using the Model View Controller ( MVC ) technique
Creating a BSP using the Model View Controller(MVC) technique
Retrieve value from input text field within a model view controller ( MVC) BSP
Web Application Development - Example code and information on development using BSP
SAP BSP / Business Server Pages combine ABAP and other web technologies such as HTML, CSS
BSP development screen - developing application using business server pages
Standard Program created in SE38 or SE80