Using MVC for BSP Uses All BSP applications that you created with SAP Web AS 6.10 can also be executed without MVC. In general, you do not need to change anything. The previous BSP implementation model gives you the option of controlling event handling and navigation using redirects. The MVC design pattern provides you with various advantages, so that you can consider converting to MVC in the following cases: •
If your pages are dynamically composed of several parts (components) A controller can assemble a page from several views. As a result, the layout is componentized.
•
If input processing is so complex that it should be subdivided into different methods A controller offers great flexibility, especially during input processing, since you can create and call new methods. If the system cannot decide which page comes next until input processing, we recommend that you let the controller branch to different views.
• • • •
If redirects using navigation can lead to performance problems (such as slow diversion) If visualization logic is fairly important, since you can use MVC to separate the logic from the layout If the layout from a different person is being processed as the visualization logic If parts of the layout should be created by the program, such as by a generating program or an XSLT processor
Combination of MVC with BSP You can combine the technology of the previous implementation model for BSPs with the new MVC design pattern. •
In an application, there may be pages with flow logic as well as controllers and views
•
The views can only be called by the controllers.
•
Redirects from pages to controllers and back can take place with the help of redirect using the navigation methods.
•
In the page layouts you can use the element or the element to call a controller. You cannot use these elements to call pages.
Process
• • • • •
Use the top controller as a point of entry to your BSP application and its process flow. First create a controller (see Creating Controllers). Then a call a view from this top controller. Next create a corresponding view (see Creating Views). Now test your controller. Then call the controller or the sub-controller (see Calling Controllers), and then the view (see Calling Views). If necessary, you can also create error pages.
Creating a Controller Use You create a controller to use a Model View Controller design pattern in your BSP application. You can use a controller for the initial access (see also Testing Controllers).
Prerequisites You are in a system from SAP Web AS 6.20.
Procedure 1. Use the Web Application Builder in the ABAP Workbench (Transaction SE80) to create a controller object as a sub-object of your BSP application. In doing so, you determine the controller name and the class name of the controller. The URL is automatically inserted from the name of the BSP application and the controller name.
2. In the Class Builder (Transaction SE24), create the class that is derived from CL_BSP_CONTROLLER2. If you create your class directly from the Web Application Builder by double-clicking on the class names, then the inheritance of CL_BSP_CONTROLLER2 has already been configured.
3. Overwrite some of the methods of your class, especially DO_REQUEST. You can find additional information about these methods in CL_BSP_CONTROLLER2 and Process Flow. 4. Save and activate your class and your controller.
Example You can find examples of controllers in BSP application ITMVC2.
Creating a View
Use You create views to use the view of the Model View Controller design pattern in your BSP application.
Prerequisites You are in a system from SAP Web AS 6.20.
Procedure 1. Use the Web Application Builder in the ABAP Workbench (Transaction SE80) to create a page with the page type View as a sub-object of your BSP application. 2. Specify the layout and any attributes that may be required. 3. Save and activate your view.
Example You can find examples of views in BSP application ITMVC2.
Testing Controllers Use You can use a controller as an initial point of entry to your BSP application. In the BSP programming model without MVC, you have always used a central page as the initial page that you called start.htm, for example. Use the main controller instead in connection with the MVC design pattern.
Prerequisites • •
You are in a system from SAP Web AS 6.20. You have successfully activated your BSP application and the controllers and views to be tested.
Procedure 1. Place your cursor on the top controller in your BSP application. 2. Click on (Test/Execute) in the application toolbar. The browser starts and a logon screen may be displayed. 3. Log on to the system if this is necessary.
Result The selected controller is started in the browser.
Calling (Sub) Controllers Use Sub-controllers can be instantiated and called by a subordinate controller (main controller) or by a view or a page.
We recommend that a subordinate controller in method DO_INIT creates a sub-controller, sets the parameters accordingly and then calls the sub-controller from the appropriate view.
Prerequisites • •
You are in a system from SAP Web AS 6.20. You have crated at least one controller for your BSP application, or at least a main and a sub-controller.
Calling a Controller 1. Create the controller instance. You have the following options: o
data: l_ctrl type ref to cl_bsp_controller. l_ctrl = create_controller( key = navigation_key ). or
o
l_ctrl = create_controller( application_namespace = 'fred' application_name = 'hugo' controller_name = 'do_something.do' ). application_namespace and application_name are optional. Unless you specify otherwise, the system uses the current values. You can also specify each of the controller IDs.
1. If necessary, set the request parameters: l_ctrl->do_initattributes( ). 2. Set additional attributes: l_ctrl->set_attribute( name = name value = value ). 3. Call the request: call_controller( l_ctrl ).
Calling (Sub) Controllers
1. Instantiate your sub-controller. Create your sub-controller in method DO_INIT: subcontroller = create_controller (controller_name = ‚name.do‘ controller_id = 'id' ). Example: flightdetails ?= create_controller( controller_name = 'flightdetails.do' controller_id = 'fld' ).
Ensure that the controller_id is specified. 2. Call your sub-controller. a. You can execute the call from the controller. This makes sense in particular if the calling controller does not control a layout. There are two call options here:
data: l_ctrl type ref to cl_bsp_controller. l_ctrl = create_controller( key = navigation_key ). or
l_ctrl = create_controller( application_namespace = 'fred' application_name = 'hugo' controller_name = 'do_something.do' ). application_namespace and application_name are optional. Unless you specify otherwise, the system uses the current values.
a. You can execute the call from the view. There are three call options here, which are all implemented using the BSP element. The comp_id used here corresponds to the controller_id from create_controller.
Note that the instantiation using the controller_id must have already taken place. A controller instance is generated if no controller is created under the comp_id. A controller instance is generated if no controller is created under the comp_id. The key is taken from the navigation table.
1. Determine the parameter transfer. a. You can execute the parameter transfer from the controller.
sub_controller -> set_attribute (name value) To do this, use any public method of the sub-controller if you know its class. Of course, you can also set the sub-controller’s public attributes directly.
a. You can execute the parameter transfer from the view.
Calling a View Prerequisites • •
You are in a system from SAP Web AS 6.20. You have created at least one view for your BSP application.
Procedure 1. Create the view instance. data: l_view type ref to if_bsp_page. l_view = create_view( key = navigation_key ). or l_view = create_view( view_name = 'next.htm' ).
Note that you can call views only from your own application. 2. Set the attributes of the view: l_view->set_attribute( name = 'model' value = my_model_instance ). or l_view->set_attribute( name = 'hugo' value = 'Hugo-Text' ). 3. Call the view layout:
call_view( l_view ).
Creating Error Pages Use In case that on a page that contains flow logic, a view, or a controller, a runtime error occurs, you can assign an error page to it. When a runtime error occurs during execution of this (other) page or this controller, the system automatically processes the assigned error page and sends it to the browser. If no error page is assigned to a page/a controller, in case of a runtime error, the system displays a standard page. If runtime errors occur in a called controller or view and there is no error page, this section remains empty.
If different types of exception occur, you can use the ERROR_OBJECT error object from class CX_ROOT in your error page. You can implement this object using dynamic ABAP and use GET_TEXT or GET_LONGTEXT methods to output an appropriate error text for your application (message short or long text). Use the functionality in the HTTP service tree (Transaction SICF) to create error pages if short dumps occur. See also Error Pages.
Prerequisites 1. • You created the page (the controller) you want to use as error page. See also: Creating Pages or Creating Controllers. 2. • You cannot assign an error page to a page or a controller that itself is marked as error page. 3. • With views you must not assign a controller class, because an error page is always called implicitly by the BSP runtime.
Procedure To identify a page / a controller as an error page: ... Select the page or the controller for your BSP application.
1. 1. 2. 2. 3. 3.
Go to the properties display and switch to Change mode if necessary. In section Error Handling, mark the checkbox Is Error Page. Save your entries and activate the page or controller.
Result You can now assign this page or controller as Assigned Error Page to another page or another controller.
Example The BSP application BSP_MODEL in the system contains an example of how to implement and use an error page.