如何在 SAPUI5 中匹配无效哈希时显示 NotFound

How to display NotFound when invalid hash is matched in SAPUI5

我执行了使用 SAPUI5 捕获和处理无效哈希的步骤,但我的应用程序无法正常工作。

当我尝试导航到更改哈希的 NotFound 视图时,我只收到一条信息消息:

但是没有显示视图。

[编辑]:

添加源代码文件:

这里我添加了绕过的部分

我已经在清单的目标部分创建了目标:

这是NotFound.controller.js

sap.ui.define([
   "my/path/controller/BaseController"
], function (BaseController) {
"use strict";

return BaseController.extend("my.path.controller.NotFound", {

    onInit: function () {
        var oRouter, oTarget;

        oRouter = this.getRouter();
        oTarget = oRouter.getTarget("NotFound");
        oTarget.attachDisplay(function (oEvent) {
            this._oData = oEvent.getParameter("data");  // store the data
        }, this);
    },

    // override the parent's onNavBack (inherited from BaseController)
    onNavBack : function (oEvent){
        // in some cases we could display a certain target when the back button is pressed
        if (this._oData && this._oData.fromTarget) {
            this.getRouter().getTargets().display(this._oData.fromTarget);
            delete this._oData.fromTarget;
            return;
        }

        // call the parent's onNavBack
        BaseController.prototype.onNavBack.apply(this, arguments);
    }

});

});

这里是NotFound.view.xml:

<mvc:View
   controllerName="my.path.controller.NotFound"
   xmlns="sap.m"
   xmlns:mvc="sap.ui.core.mvc">
   <MessagePage
      title="{i18n>NotFound}"
      text="{i18n>NotFound.text}"
      description="{i18n>NotFound.description}"
      showNavButton="true"
      navButtonPress="onNavBack"/>
</mvc:View>

这里是 App 控制器的 onInit 方法:

onInit: function(){
        jQuery.sap.log.setLevel(jQuery.sap.log.Level.INFO);

        var oRouter = this.getRouter();

        oRouter.attachBypassed(function (oEvent) {
            var sHash = oEvent.getParameter("hash");
            // do something here, i.e. send logging data to the backend for analysis
            // telling what resource the user tried to access...
            jQuery.sap.log.info("Sorry, but the hash '" + sHash + "' is invalid.", "The resource was not found.");
        });

        oRouter.attachRouteMatched(function (oEvent){
            var sRouteName = oEvent.getParameter("name");
            // do something, i.e. send usage statistics to backend
            // in order to improve our app and the user experience (Build-Measure-Learn cycle)
            jQuery.sap.log.info("User accessed route " + sRouteName + ", timestamp = " + new Date().getTime());
        });
    }

和 任何人都可以帮助我吗? 此致,

检查这个插件:

https://plnkr.co/edit/pxOkRSM8c97hXO6gkbpV

关键配置是manifest.json:

 "targets": {
                "tgHome": {
                    "viewPath": "sapui5Example",
                    "viewName": "home"
                },
                "notFound": {
                    "viewPath": "sapui5Example",
                    "viewName": "NotFound",
                    "transition": "show"
                }
            }

要触发 'not found' 路由,请下载 plunker 并在 URL 中,在散列后键入任何内容,您将看到未找到的页面(如果您直接在 plunker 上执行此操作不会工作)。这是一张照片: