Skip to Content

If you want to handle events within ABAP WebDynpro which are being raised in a referenced class, you can build a custom event handler class (or use your assistance class).  It would be nice if the event handler class could be replaced with event handlers within the ABAP WebDynpro environment, but events in ABAP WebDynpro and events in standalone classes are handled (or referenced) slightly different.

  

I’ll provide a small (easy) demonstration and if anyone knows of a way to handle the events directly from within the ABAP WebDynpro and bypass the handler class, please share.  Thanks..

Step 1: Let’s create a basic class with a method to simulate validation of input entered on a view.

                Create Class:  ZCL_TEST_EVENTS_FOR_WD_GUI

                    /wp-content/uploads/2013/01/pic__177478.jpg

                Create Event:  BAD_INPUT with event parameter

                    /wp-content/uploads/2013/01/pic__177478.jpg

     Event Parameter

       /wp-content/uploads/2013/01/pic__177478.jpg

                Create Method:  VALIDATE_INPUT with code to raise the event

                    /wp-content/uploads/2013/01/pic__177478.jpg

          method validate_input.
                    data lt_messages type z_tt_tline.
                    raise event bad_input
                    exporting
                              it_messages
= lt_messages.
          endmethod.

Step 2:  Now create a basic class with methods to handle the event BAD_INPUT.

                Create Class:  ZCL_TEST_EVENT_HANDLERS_WD_GUI

                     /wp-content/uploads/2013/01/pic__177478.jpg

                Create Attribute:  G_WD_CONTEXT Type Ref To IF_WD_CONTEXT_NODE

                     /wp-content/uploads/2013/01/pic__177478.jpg

                Create Method:  CONSTRUCTOR which will setup our event handler

                    /wp-content/uploads/2013/01/pic__177478.jpg

          method constructor.
                    set handler on_bad_input for all instances.
          endmethod.

                Create Method:  SET_CONTEXT which we will use to pass the view context

                     /wp-content/uploads/2013/01/pic__177478.jpg

                    method set_context.
                              g_wd_context
= p_wd_context.
                    endmethod.

                Create Method:  ON_BAD_INPUT which we will designate our event handler

                    /wp-content/uploads/2013/01/pic__177478.jpg

          method on_bad_input.
                    data lo_nd_tline type ref to if_wd_context_node.
                    data lt_tline type z_tt_tline.
                    data l_line type tline.

                    l_linetdline = ‘Some Error Message 1’.
                    append l_line to lt_tline.
                    l_linetdline = ‘Some Error Message 2’.
                    append l_line to lt_tline.

                                 l_linetdline = ‘Some Error Message 3’.
                                 append l_line to lt_tline.

                                 lo_nd_tline
= g_wd_context->get_child_node( name = ‘Z_TT_TLINE’ ).
                                 lo_nd_tline
->bind_table( new_items = lt_tline set_initial_elements = abap_true ).
                    endmethod.

     Note:
     Using this icon: /wp-content/uploads/2013/01/pic__177478.jpg
will allow you to designate your method as an event handler for the class and event you already created above.

     It should be defined as follows:

      /wp-content/uploads/2013/01/pic__177478.jpg    

         Add the following parameter to your event handler since we configured this parameter to be passed by the event above.

      /wp-content/uploads/2013/01/pic__177478.jpg

Step 3:  Testing what we created from within an ABAP WebDynpro Application.

                Create WD Application:  ZEXTERNAL_EVENTS

                /wp-content/uploads/2013/01/pic__177478.jpg    

                Add the messaging structure to the context of your view (cardinality 0..n):

                /wp-content/uploads/2013/01/pic__177478.jpg    

                Add a button to your layout so we can simulate data being saved:

                /wp-content/uploads/2013/01/pic__177478.jpg

                With the following OnAction Event:

                /wp-content/uploads/2013/01/pic__177478.jpg

                Add a table to your layout and bind it to your context node you just created:

                /wp-content/uploads/2013/01/pic__177478.jpg

                In the validate method, add the following code:

method onactionvalidate .
*************************************************************
* Setup Event Handling
*************************************************************
 
if wd_this->create_event_handlers is not bound.
   
create object wd_this->create_event_handlers.
 
endif.
*************************************************************

*************************************************************
* Create an Instance of the Class which will fire events
*************************************************************
 
if wd_this->test_events is not bound.
   
create object wd_this->test_events.
 
endif.
*************************************************************

*************************************************************
  wd_comp_controller
->create_event_handlers->set_context( wd_context ).
*************************************************************

*************************************************************
  wd_comp_controller
->test_events->validate_input( ).
*************************************************************
endmethod.

Test your application (to fire the event select your button).  These are the results:

               /wp-content/uploads/2013/01/pic__177478.jpg

To report this post you need to login first.

3 Comments

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

  1. Christopher Solomon

    I think I am following you…but does this mean a “listener” can only listen to events from its own or ONE other class? You can’t just listen for a generic event and do what you will with it?

    (0) 
    1. Philip Johnston Post author

      You can have as many listeners as you want.  If you wanted to handle a generic event, the handler would have to listen to the same signature which you would need to define.  Similar to the try/catch exception handler I assume..  (:->

      (0) 
      1. Christopher Solomon

        I guess I am asking it wrong…I mean in the opposite way…..yes, I can see ONE class firing an event and MANY classes “listening” for the event from THAT class…..but can I have a “listener” class that listens for a generic “event” that can come from MANY classes?

        (0) 

Leave a Reply