Assigning a View Controller
To assign a controller to an XML view, the attribute
controllerName can be used in the
<View> tag (see the figure Referencing a Controller). The name of the controller class is entered as the value of the attribute.
SAPUI5 loads the controller via the module system.
The XML view shown in the figure is taken from a project in which the
webapp folder is registered in the bootstrap script as resource location. For this purpose, the module Id prefix
sap.training.exc is assigned to the
webapp folder via the
data-sap-ui-resourceroots attribute in the bootstrap script.
The controller name
sap.training.exc.controller.App is therefore resolved as follows: The prefix
sap.training.exc points to the
webapp folder. The
controller segment following
sap.training.exc specifies the
controller subfolder as the location of the file to load. The last segment in the controller name represents the file name, whereby the suffix
.controller.js is automatically appended. That is, SAPUI5 ultimately loads the file
App.controller.js from the
Typically, view controllers are stored in the
controller folder of the project structure. More information about the implementation of a view controller follows in the next section.
Many controls can trigger events. For example, a
sap.m.Button triggers the event
press when the user clicks or taps the control. Which events are supported can be looked up in the API Reference in the Events section for the respective control.
In an XML view, event handlers can be attached to events. To do this, an attribute with the name of the event is added to the corresponding control tag, for example, a
press attribute to a
<Button> tag. The attribute value is then the name of the event handler.
Event handler names that begin with a dot ('.') are assumed to represent a method in the controller. They are resolved by removing the leading dot and calling the method with the resulting name on the controller instance. So, in the example shown, the method
onSayHello is called on the view controller when the user clicks or taps the button.