Additional Blogs by SAP
cancel
Showing results for 
Search instead for 
Did you mean: 
craigcmehil
Community Manager
Community Manager
0 Kudos

Building your Controllers and Classes

Now for the most part we tend to start off with building our pages then the controller behind it, simply because it is often easier in terms of variable names and programming. You do your layout then build up the backend logic. But since I'm writing this after the fact, OK, twice after the fact I decided to do it in reverse order. Now please pay close attention to these parts as they are different than the original programming in many ways. The basic logic is there but the rest is quite different.



Now as you can see from this image I've added in several more classes than we had previously. Which means that yes with an MVC concept there is a bit more programming, but the benefits I hope you will see by the time we are done with the whole process.

What we have here is a Controller class for each section of the application, within these classes all of the logic and processing we will want to accomplish is done. Now I've purposely kept these to a minimum in order to give each of you a chance to play around and find ways of improving not only performance but also the logic.

Each of the following classes have the Superclass of CL_BSP_CONTROLLER2 and some have even the Interface IF_HTMLB_TABLEVIEW_ITERATOR. Using the IF_HTMLB_TABLEVIEW_ITERATOR interface will allow us to create our Table Iterators as part of the class itself, useful when the View you are associating with the class has only one table on it.

So let's get started with creating our first controller. Basically on your BSP application select "Create" then "Controller".



Once it is there then give it the "Controller" name, again try for a naming convention that will make sense later on.



Once you type that in and save it then you can double click on the controller name and it will prompt you to create the controller class.

Now go ahead and create each of the following controllers and then their respective classes. Once that is done we'll go through and fill in our data, attributes, interfaces and coding.



Now that they are all there let's go to the our Main controller, ZFAQ_C_CL_FAQ.

Attributes

These are basically like our Page attributes.

MODELInstance AttributePrivateType Ref ToZFAQ_CL_M
LT_TABInstance AttributeProtectedTypeSTRING
CTR_INTROInstance AttributeProtectedType Ref ToCL_BSP_CONTROLLER2
CTR_CATInstance AttributeProtectedType Ref ToCL_BSP_CONTROLLER2
CTR_SUBJECTInstance AttributeProtectedType Ref ToCL_BSP_CONTROLLER2
CTR_SDNLINKSInstance AttributeProtectedType Ref ToCL_BSP_CONTROLLER2
CTR_SDNInstance AttributeProtectedType Ref ToCL_BSP_CONTROLLER2
CTR_INDEXInstance AttributeProtectedType Ref ToCL_BSP_CONTROLLER2

Now we are going over to the Methods tab and we are going to "redefine" the methods that we will be using.

DO_INIT

> method DO_INIT .

model ?= create_model( class_name = 'ZFAQ_CL_M'
model_id = 'mf' ).

ctr_intro ?= create_controller( controller_name = 'intro.do'
controller_id = 'intro' ).

ctr_intro->set_model( model_id = 'mf'
model_instance = model ).

ctr_cat ?= create_controller( controller_name = 'cat.do'
controller_id = 'cat' ).

ctr_cat->set_model( model_id = 'mf'
model_instance = model ).

ctr_subject ?= create_controller( controller_name = 'subject.do'
controller_id = 'subject' ).

ctr_subject->set_model( model_id = 'mf'
model_instance = model ).

ctr_sdnlinks ?= create_controller( controller_name = 'sdnlinks.do'
controller_id = 'sdnlinks' ).

ctr_sdnlinks->set_model( model_id = 'mf'
model_instance = model ).

ctr_sdn ?= create_controller( controller_name = 'sdn.do'
controller_id = 'sdn' ).

ctr_sdn->set_model( model_id = 'mf'
model_instance = model ).

ctr_index ?= create_controller( controller_name = 'index.do'
controller_id = 'index' ).

ctr_index->set_model( model_id = 'mf'
model_instance = model ).

endmethod.

 
DO_INITATTRIBUTES

> method DO_INITATTRIBUTES .

lt_tab = request->get_form_field( 'faq_tabs' ).
if lt_tab IS INITIAL.
lt_tab = model->GET_TAB( ).
if lt_tab IS INITIAL.
lt_tab = 'overview'.
endif.
endif.

endmethod.

 
DO_REQUEST

> method DO_REQUEST .

* Data definitions
data: default_view type ref to if_bsp_page.

* Start event handling
dispatch_input( ).

* Create view
default_view = create_view( view_name = 'faq.htm' ).

default_view->set_attribute( name = 'lv_tab'
value = lt_tab ).

* Call view
call_view( default_view ).

if lt_tab is not initial.
model->SET_TAB( tab = lt_tab ).
endif.

endmethod.

 
DO_HANDLE_EVENT

> method DO_HANDLE_EVENT .

* Object definitions
data: lt_event TYPE REF TO if_htmlb_data.
data: tabStrip TYPE REF TO CL_XHTMLB_TABSTRIP.

lt_event = cl_htmlb_manager=>get_event_ex( request ).

tabStrip ?= CL_HTMLB_MANAGER=>GET_DATA( request = request
name = 'xhtmlb:tabstrip'
id = 'faq_tabs' ).

IF lt_event IS NOT INITIAL.

lt_tab = tabStrip->selection.

ENDIF.

endmethod.

 










Now before we move to far further along, I want to bring your attention back to DO_INIT and to DO_REQUEST. In DO_INIT you'll notice we define in there the rest of our controllers, the reason for this is because they are basically sub controllers and exist only within the realm of the main controller and therefore can not be accessed on their own. In the DO_REQUEST you'll notice a line dispatch_input() now this method controls the processing of each controller and should only be listed within the main controller and not the sub controllers. Another important item to remember to to check the "Stateful" option in your BSP Application.



Otherwise the data that we set in the Model will not stay and we'll loose the values each time the page is worked with.

So with those items in place we can now finish the rest of our controllers.

ZFAQ_C_CL_INTRO

Attributes

MODELInstance AttributePrivateType Ref ToZFAQ_CL_M


Methods

DO_REQUEST

> method DO_REQUEST .

* Data definitions
data: default_view type ref to if_bsp_page.

* Create view
default_view = create_view( view_name = 'info/intro.htm' ).

* Call view
call_view( default_view ).

endmethod.