Skip to Content

BSP MVC Template

This is a template which i use almost every time when ever i have to build a MVC BSP application. Helps me a lot though. Our aim in this blog would be to build a Template application where we have 2 controllers and one model. One Controller Calls another one with parent and child relationship.
!https://weblogs.sdn.sap.com/weblogs/images/251689026/1.jpg|height=262|alt=image|width=487|src=https://weblogs.sdn.sap.com/weblogs/images/251689026/1.jpg|border=1!

h3. Create a test package ZMVC_TEMPLATE

In this package we will store all the objects which we will be creating.

!https://weblogs.sdn.sap.com/weblogs/images/251689026/2.jpg|height=175|alt=image|width=448|src=https://weblogs.sdn.sap.com/weblogs/images/251689026/2.jpg|border=0!

Lets get on to the job and start creating the Objects. The most important object in the application would be the classes which would act as the backbonbe of the application.

h3. CLASSES</u>

Model

Create Model Class ZMVC_TEM_MODEL
!https://weblogs.sdn.sap.com/weblogs/images/251689026/3.jpg|height=243|alt=image|width=598|src=https://weblogs.sdn.sap.com/weblogs/images/251689026/3.jpg|border=0!

Alternate to create classes would be transaction code SE24, but staying in SE80 would be a goos idea so we see all the objects which we create in Tree View.

Leave the other settings as default.!https://weblogs.sdn.sap.com/weblogs/images/251689026/4.jpg|height=75|alt=image|width=338|src=https://weblogs.sdn.sap.com/weblogs/images/251689026/4.jpg|border=0!

Assign a superclass CL_BSP_MODEL to the class by clicking on the !https://weblogs.sdn.sap.com/weblogs/images/251689026/up.png|height=25|alt=image|width=26|src=https://weblogs.sdn.sap.com/weblogs/images/251689026/up.png|border=0!  Icon (in the properties tab) and then save the class. 
!https://weblogs.sdn.sap.com/weblogs/images/251689026/5.jpg|height=120|alt=image|width=345|src=https://weblogs.sdn.sap.com/weblogs/images/251689026/5.jpg|border=0!

+ Note: Make sure you assign the class to the same package which we created otherwise we will be lost.+

h3. Parent Controller

Copy the class ZMVC_TEM_MODEL to ZMVC_TEM_PARENT.<br />!https://weblogs.sdn.sap.com/weblogs/images/251689026/6.jpg|height=206|alt=image|width=418|src=https://weblogs.sdn.sap.com/weblogs/images/251689026/6.jpg|border=0!

!https://weblogs.sdn.sap.com/weblogs/images/251689026/7.jpg|height=133|alt=image|width=451|src=https://weblogs.sdn.sap.com/weblogs/images/251689026/7.jpg|border=0!

Change the inheritance of the class from CL_BSP_MODEL to CL_BSP_CONTROLLER2 by clicking on  image button on the Properties tab of the copied class. To avoid confusion change the desciption of the class as well to MVC Parent Controller.
!https://weblogs.sdn.sap.com/weblogs/images/251689026/9.jpg|height=156|alt=image|width=499|src=https://weblogs.sdn.sap.com/weblogs/images/251689026/9.jpg|border=0!

Create a reference variable to the model In the attributes of the class.
!https://weblogs.sdn.sap.com/weblogs/images/251689026/10.jpg|height=123|alt=image|width=628|src=https://weblogs.sdn.sap.com/weblogs/images/251689026/10.jpg|border=0!

Save the Class.

h3. Child Controller

 Copy the class ZMVC_TEM_PARENT to ZMVC_TEM_CHILD.
!https://weblogs.sdn.sap.com/weblogs/images/251689026/copyparent.png|height=219|alt=image|width=588|src=https://weblogs.sdn.sap.com/weblogs/images/251689026/copyparent.png|border=1!

 !https://weblogs.sdn.sap.com/weblogs/images/251689026/12.jpg|height=136|alt=image|width=456|src=https://weblogs.sdn.sap.com/weblogs/images/251689026/12.jpg|border=0!

No need to change the inheritance of the class as its copied from Parent class class which extends to CL_BSP_MODEL. To avoid confusion change the desciption of the class as well to MVC Child Controller.
!https://weblogs.sdn.sap.com/weblogs/images/251689026/13.jpg|height=161|alt=image|width=484|src=https://weblogs.sdn.sap.com/weblogs/images/251689026/13.jpg|border=0!

Create a reference variable to the model In the attributes of the class.
!https://weblogs.sdn.sap.com/weblogs/images/251689026/14.jpg|height=118|alt=image|width=625|src=https://weblogs.sdn.sap.com/weblogs/images/251689026/14.jpg|border=0!

Save the Class

*SAVE AND ACTIVATE ALL THE OBJECT WHICH WE HAVE CREATED SO FAR. *

 

h3. BSP APPLICATION

 

Create a BSP Application and name it ZMV_TEM_BSP.
!https://weblogs.sdn.sap.com/weblogs/images/251689026/15.jpg|height=210|alt=image|width=623|src=https://weblogs.sdn.sap.com/weblogs/images/251689026/15.jpg|border=0!

!https://weblogs.sdn.sap.com/weblogs/images/251689026/16.jpg|height=115|alt=image|width=608|src=https://weblogs.sdn.sap.com/weblogs/images/251689026/16.jpg|border=0!

Note: Make sure we make application Stateful.
!https://weblogs.sdn.sap.com/weblogs/images/251689026/17.jpg|height=352|alt=image|width=601|src=https://weblogs.sdn.sap.com/weblogs/images/251689026/17.jpg|border=0!

h3. CONTROLLERS

Lets create the parent / child (controller/views) and do class assignments.

 

h3. Parent Controller

!https://weblogs.sdn.sap.com/weblogs/images/251689026/18.jpg|height=167|alt=image|width=423|src=https://weblogs.sdn.sap.com/weblogs/images/251689026/18.jpg|border=0!

 !https://weblogs.sdn.sap.com/weblogs/images/251689026/19.jpg|height=130|alt=image|width=598|src=https://weblogs.sdn.sap.com/weblogs/images/251689026/19.jpg|border=0!

Assign the Controller class ZMV_TEM_PARENT to the controller which we just created.
!https://weblogs.sdn.sap.com/weblogs/images/251689026/21.jpg|height=191|alt=image|width=600|src=https://weblogs.sdn.sap.com/weblogs/images/251689026/21.jpg|border=0!

+NOTE:Leave rest of the settings as default. +

h3. Child Controller

!https://weblogs.sdn.sap.com/weblogs/images/251689026/22.jpg|height=167|alt=image|width=426|src=https://weblogs.sdn.sap.com/weblogs/images/251689026/22.jpg|border=0!

!https://weblogs.sdn.sap.com/weblogs/images/251689026/23.jpg|height=132|alt=image|width=601|src=https://weblogs.sdn.sap.com/weblogs/images/251689026/23.jpg|border=0!

Assign the Controller class ZMV_TEM_CHILD to the controller which we just created.
!https://weblogs.sdn.sap.com/weblogs/images/251689026/24.jpg|height=163|alt=image|width=583|src=https://weblogs.sdn.sap.com/weblogs/images/251689026/24.jpg|border=0!

 

h3. VIEWS

Parent View

Lets name the parent view as parent.htm

!https://weblogs.sdn.sap.com/weblogs/images/251689026/25.jpg|height=181|alt=image|width=468|src=https://weblogs.sdn.sap.com/weblogs/images/251689026/25.jpg|border=0!

 !https://weblogs.sdn.sap.com/weblogs/images/251689026/26.jpg|height=236|alt=image|width=597|src=https://weblogs.sdn.sap.com/weblogs/images/251689026/26.jpg|border=0!

Make sure you select the radio button View.

Assign the Parent class to the view as well.
!https://weblogs.sdn.sap.com/weblogs/images/251689026/27.jpg|height=253|alt=image|width=558|src=https://weblogs.sdn.sap.com/weblogs/images/251689026/27.jpg|border=0!

In the Page attributes give a reference to the model class.
!https://weblogs.sdn.sap.com/weblogs/images/251689026/28.jpg|height=131|alt=image|width=512|src=https://weblogs.sdn.sap.com/weblogs/images/251689026/28.jpg|border=0!

 In the layout of the parent.htm call the child.do controller.h3. Child View

Lets name the child view as child.htm

Repeat the steps of Parent View. Make sure you assign Child controller class ZMVC_TEM_CHILD to the child view in the properties tab.

In the layout of the child.htm paste the following code. Here we don’t another content/page/form tag as this view will be called inside the parent view(generating one html page) so we can’t have 2 such similar tags on one page.h2. CODE

*

*

 

h3. *ZMVC_TEM_PARENT *

Go to ZMVC_TEM_PARENT class and do the following.
Uncheck the filter checkbox to get a list of all the methods inherited form CL_BSP_CONTROLLER class and redefine following methods.
DO_INIT<br />DO_REQUEST<br />DO_HANDLE_EVENT<br />!https://weblogs.sdn.sap.com/weblogs/images/251689026/33.jpg|height=106|alt=image|width=632|src=https://weblogs.sdn.sap.com/weblogs/images/251689026/33.jpg|border=0!

 To redefine methods select the method and click on the button !https://weblogs.sdn.sap.com/weblogs/images/251689026/34.png|height=23|alt=image|width=30|src=https://weblogs.sdn.sap.com/weblogs/images/251689026/34.png|border=0!

+Hint: After redefinition is done the color of the method will change from Blue to Black :). You can check the mark of Filter again to avoid big list of unwanted methods. </p><p>!https://weblogs.sdn.sap.com/weblogs/images/251689026/35.png|height=268|alt=image|width=586|src=https://weblogs.sdn.sap.com/weblogs/images/251689026/35.png|border=0!</p><p>Note: Repeat the steps for the Child class (ZMVC_TEM_CHILD) as well.+
ZMVC_TEM_PARENT
DO_INIT  This method will create instance of the model class

METHOD do_init.
  me->model ?= create_model( class_name = ‘ZMVC_TEM_MODEL’
                             model_id   = ‘Z’ ).
ENDMETHOD.

DO_REQUEST This method generates the view
METHOD do_request.
* datadeclaration
  DATA: parent      TYPE REF TO if_bsp_page.
* if input is available, dispatch this input to subcomponent.
* this call is only necessary for toplevel controllers.
  dispatch_input( ).
* if any of the controllers has requested a navigation,
* do not try to display, but leave current processing
  IF is_navigation_requested( ) IS NOT INITIAL.
    RETURN.
  ENDIF.
* output current view (create, set attibutes, and call)
  parent = create_view( view_name = ‘parent.htm’ ).
* set attribute model if needed
  parent->set_attribute( name = ‘model’ value = model ).
* call master views
  call_view( parent ). “calls itself
ENDMETHOD.

h3. ZMVC_TEM_CHILD

DO_INIT This method will set the model, equalize the model instance of parent and child. No need to recreate the model as the parent already did it.
METHOD do_init.
  DATA: lr_my_parent TYPE REF TO zmvc_tem_parent.
* set (MVC) model
  lr_my_parent ?= me->m_parent.
  set_model( model_instance = lr_my_parent->model
             model_id = ‘Z’ ).
  me->model = lr_my_parent->model.
ENDMETHOD.

DO_REQUEST – This method generates the view
METHOD do_request.
* datadeclaration
  DATA: child      TYPE REF TO if_bsp_page.
* if input is available, dispatch this input to subcomponent.
* this call is only necessary for toplevel controllers.
  dispatch_input( ).
* if any of the controllers has requested a navigation,
* do not try to display, but leave current processing
  IF is_navigation_requested( ) IS NOT INITIAL.
    RETURN.
  ENDIF.
* output current view (create, set attibutes, and call)
  child = create_view( view_name = ‘child.htm’ ).
* set attribute model if needed
  child ->set_attribute( name = ‘model’ value = model ).
* call master views
  call_view( child ). “calls itself
ENDMETHOD.

DO_HANDLE_EVENT This method handles the events fired from the respective view
METHOD do_handle_event.
  DATA: lr_event        TYPE REF TO if_htmlb_data.

* load htmlb_manager
  CLASS cl_htmlb_manager DEFINITION LOAD.
* try to get a normal event
  lr_event = cl_htmlb_manager=>get_event( request ).
try to get extended event<br />  IF lr_event IS INITIAL.<br />    lr_event = cl_htmlb_manager=>get_event_ex( request ).<br />  ENDIF.<br /><br /> check if system have send an event
  IF lr_event IS INITIAL.
    RETURN.
  ENDIF.
* Controller/View EVENT processing ….
  CASE lr_event->event_name.
**********************************************************************<br />    WHEN ‘button’.<br />    WHEN ‘radioButton’.<br />  ENDCASE.<br />ENDMETHOD.*</p>h2.  

RESULTS

Execute parent.do controller (as it would be the starting point of our application and will call the child controller)and see the results,
!https://weblogs.sdn.sap.com/weblogs/images/251689026/36.png|height=235|alt=image|width=629|src=https://weblogs.sdn.sap.com/weblogs/images/251689026/36.png|border=0!

 

We extend the hirarchy to a much deeper level by defining another child of parent or defininig child of child.

<br /> </p>

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply