Additional Blogs by Members
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member
0 Kudos

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:...!

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:...!

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:...!

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:/...!

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:/...!  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:...!

+ 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:...!

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

Change the inheritance of the class from CL_BSP_MODEL to CL_BSP_CONTROLLER2 by clicking on  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:...!

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...!

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|s...!

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

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...!

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...!

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...!

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

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...!

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...!

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

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...!

+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...!

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

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...!

 

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...!

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

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...!

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...!

 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...!

 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:/...!

+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...!</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...!

 

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

<br /> </p>