SAPUI5 Using XML-File as View with “data-sap-ui-re

2020-04-16 06:01发布

I'm doing the SAPUI5 Walkthrough and stuck at step 4. (Walkthrough Step 4)

I'm working with Eclipse and don't know how to change this code-line so it works for my project and is going to find my view.

         data-sap-ui-resourceroots='{
        "sap.ui.demo.wt": "./"
     }' 

I need to know what to insert for "sap.ui.demo.wt" when using an Eclipse project.

Thanks for any hints :)

EDIT:

Now I got a working page with a button which triggers a pop-up.

Folder structure:

SAPUI5_Test
 - WebContent
    - controller
       -> NewView.controller.js
    - view
       -> NewView.view.xml
    - index.html

index.html:

    <!DOCTYPE html>
<html>
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <meta charset="UTF-8">
    <title>SAPUI5 Walkthrough</title>
    <script
        id="sap-ui-bootstrap"
        src="https://openui5.hana.ondemand.com/resources/sap-ui-core.js"
        data-sap-ui-theme="sap_bluecrystal"
        data-sap-ui-libs="sap.m"
        data-sap-ui-compatVersion="edge"
        data-sap-ui-preload="async"
        data-sap-ui-resourceroots='{
            "SAPUI5_Test": "./"
        }'>
    </script>
    <script>
        sap.ui.getCore().attachInit(function () {
            sap.ui.xmlview({
                viewName: "SAPUI5_Test.view.NewView"
            }).placeAt("content");
        });
    </script>
</head>
<body class="sapUiBody" id="content">
</body>
</html>

NewView.view.xml:

<mvc:View
controllerName="SAPUI5_Test.controller.NewView"
xmlns="sap.m"
xmlns:mvc="sap.ui.core.mvc">
<Button 
text="Say hello"
press="onShowHello"/>
</mvc:View>

NewView.controller.js

sap.ui.controller("SAPUI5_Test.controller.NewView", {
    onShowHello : function() {
        alert("Hello SAPUI5_Controller");
    }
});

So thanks for the help! And maybe this could help someone in the future :)

1条回答
闹够了就滚
2楼-- · 2020-04-16 06:25

SAPUI5 is searching for resources like views, controllers or other classes by their full names.

It is assuming that every namespace part is a folder and the class/view name is the filename. If its a view the filename has to be xxx.view.xml. if its a controller the filename has to be xxx.controller.js.

The root folder where it starts its search is by default the folder of the sap-ui-core.js.

You can change the root folder by defining a data-sap-ui-resourceroots mapping. Its a javascript object that maps a namespace to a path (relative to the index.html).

With the following bootstrap tag company.myProject.view.View1 is searched for at /resources/company/myProject/view/View1.view.xml:

<script id="sap-ui-bootstrap" 
    src="resources/sap-ui-core.js"></script>

With this bootstrap tag company.myProject.View1 is searched for at /view/View1.view.xml:

<script id="sap-ui-bootstrap" 
    src="resources/sap-ui-core.js"
    data-sap-ui-resourceroots='{
      "company.myProject": "./" }'></script>

And with this bootstrap tag company.myProject.View1 is searched for at ddd/myProject/view/View1.view.xml:

<script id="sap-ui-bootstrap" 
    src="resources/sap-ui-core.js"
    data-sap-ui-resourceroots='{
      "company": "./ddd" }'></script>

Edit: Please beware that you have to use " inside of the map. ' will not work.


Edit: Clarification of folder structure

I would recommend that you put everything in a project namespace. Use this folder structure:

WebContent
- controller
  - NewView.controller.js
- view
  - NewView.view.xml
- index.html
  • Rename View folder to view (namespaces are camelCase by convention).
  • Rename newView.view.xml to NewView.view.xml (Classes/Views are PascalCase by convention)
  • Use data-sap-ui-resourceroots='{ "myproject": "./" }'.
  • Change your Controller name to myproject.controller.NewView: Controller.extend("myproject.controller.NewView", {...}); or sap.ui.controller("myproject.controller.NewView",{...});.
  • In the xmlview use controllerName="myproject.controller.NewView".
  • Instantiate the view with sap.ui.xmlview({ viewName: "myproject.view.NewView"}).placeAt("content");.

With that you don't have to add a mapping for every subfolder in your project.

查看更多
登录 后发表回答