Creating a BSP using the Model View Controller(MVC) technique
Tutorial 3 - Event handling and calling a new view
Step 1 - Redefine DO_HANDLE_EVENT event
Return to the controller class you created in tutorial 1, accessed via the controller page (i.e. main.do)
and double clicking on the cc name ( ZCL_CONTROLLER_01 ). Go into change mode and find the
DO_HANDLE_EVENT method and redefine it.
Step 2 - Insert code in to DO_HANDLE_EVENT
Enter the following ABAP code which handles a button click event:
method DO_HANDLE_EVENT .
*CALL METHOD SUPER->DO_HANDLE_EVENT
* EXPORTING
* EVENT =
* HTMLB_EVENT =
** HTMLB_EVENT_EX =
* GLOBAL_MESSAGES =
* RECEIVING
* GLOBAL_EVENT =
* .
DATA: button_event TYPE REF TO CL_HTMLB_EVENT_BUTTON. "date event
DATA: date_event TYPE REF TO CL_HTMLB_EVENT_DATENAVIGATOR. "button event
* Check if event being processed is a button event
IF htmlb_event IS BOUND AND htmlb_event->name = 'button'.
* Use widening cast to take generic event to specific event (button event)
* - Basically moves current event structure into button event structure,
* - so that the button event now contains the relevant data
button_event ?= htmlb_event.
*
* Contains value store in the 'onClick' parameter on page view
if button_event->server_event = 'myClickHandler'.
page = 'page2.htm'.
endif.
ENDIF.
* Check if event being processed is a date event
* - the below code is simply for further demonstration of above syntax
IF htmlb_event IS BOUND AND htmlb_event->name = 'dateNavigator'.
date_event ?= htmlb_event.
ENDIF.
endmethod.
Step 3 – Create attributte to store next page value
Return back to Class interface and define a new class attributte as type string to store next page
value!
Step 4 – Modify DO_REQUEST method
You now need to modify the DO_REQUEST code so that it calls the event handling and controls which
page to display based on the new page variable/attribute. The event handling is called using the
'dispatch_input( )' command.
METHOD do_request .
*CALL METHOD SUPER->DO_REQUEST
* .
DATA: r_view TYPE REF TO if_bsp_page.
* Calls event handler DO_HANDLE_EVENT
dispatch_input( ).
IF page EQ 'main1.htm' or page EQ space.
r_view = create_view( view_name = 'main1.htm' ).
r_view->set_attribute( name = 'p_ord'
value = me->r_model ).
ELSEIF page = 'page2.htm'.
r_view = create_view( view_name = 'page2.htm' ).
r_view->set_attribute( name = 'p_ord'
value = me->r_model ).
ENDIF.
** Create object r_view with view_name main1.htm
** Layout is provided by view main1.htm
* r_view = create_view( view_name = 'main1.htm' ).
* r_view->set_attribute( name = 'p_ord'
* value = me->r_model ).
call_view( r_view ).
ENDMETHOD.
Step 5 – Create second View
Firstly save and activate the controller class. The next stage is to create the second view which
is executed from within DO_REQUEST. This will need to be called 'page2.htm' unless you modify the
code you have just placed in the DO_REQUEST method. The simplest way to do this is to copy your
existing view (main1.htm), You might want to change some text slightly so that you can distanguish
between the 2 page.
i.e. change PO text to 'Purchase order2'.
Step 6 – Save and activate
Ensure you save and activate all the objects that have been changed during this tutorial.
Tutorial 1 Tutorial 2 Tutorial 3
|