SAPUI5 (36) - OData Model 连接后端 SAP 系统 (下)

2024-02-05 13:48

本文主要是介绍SAPUI5 (36) - OData Model 连接后端 SAP 系统 (下),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

继续上一篇的内容,完成使用 OData Model 连接到后端 SAP 系统,实现 CRUD 操作。

程序界面:

点击 Create 按钮,弹出对话框:

输入 id, name 和 Address,点击 Save 按钮保存数据,点击 Cancel 按钮取消。

单击 table 中某行后,点击 Edit 按钮,弹出对话框:

可以进行修改操作。

单击 table 中某行后,点击 Delete 按钮,提示确认删除对话框,可以进行删除操作。

要点:

  • SAP Web IDE 实现代理
  • 配置数据源
  • 连接到 SAP 后端并实现 CRUD 操作

SAP Web IDE 代理配置

我使用的 IDE 是 Web IDE personal edition,如果把 Web IDE 的安装目录称作 webide_home 的话,我们需要在 webide_home\config_master\service.destinations\destinations 下配置连接,这个连接对于所有 Project 都可以使用。请参考本系列的第 33 篇。

本次我们连接的后端系统标识为 DPH,所以我们的配置文件名为 DPH,没有扩展名。配置文件的内容如下:

Description=DP Hana
Type=HTTP
TrustAll=true
Authentication=NoAuthentication
WebIDEUsage=odata_abap,dev_abap,ui5_execute_abap
Name=DPH
WebIDEEnabled=true
URL=HTTP\://dph01.nodomain\:8180
ProxyType=OnPremise
WebIDESystem=DPH
sap-client=100

配置数据源

在 SAP Web IDE 中,创建类型为 SAPUI5 Application 的项目,这种类型项目的文件结构相对来说是最简单的。创建完成后,项目的文件结构如下:

配置 neo-app.json 文件

在 neo-app.json 文件中,增加一项 path 配置,内容如下:

    {"path": "/sap/opu/odata","target": {"type": "destination","name": "DPH","entryPath": "/sap/opu/odata"},"description": "DP Hana"}

配置 Application descriptor

Application descriptor 就是 webapp 下面的 manifest.json 文件,使用 App Descriptor Editor 打开,切换到 Data Sources 页签,点击 “+” 号来添加一个数据源:

系统弹出对话框简化配置,切换到 Service URL,选择在前面配置的 SAP 连接,第一行 DP Hana 保存的是 domain 信息,第二行配置的是 service url 的 path:

点击 Test 按钮,提示输入用户名和密码,如果一切 OK, 系统读取到 OData service 并且加载:

选中 EmployeeCollection ,点击 Next 按钮即可完成配置。

metadata.xml 文件会被配置到 model 文件夹下面。但我的 Web IDE 版本将实际文件放在 localServices 文件夹下面,需要手工调整位置。这个文件也可以通过在浏览器中 $metadata 参数的方法得到。

然后在 manifest.json 文件中增加默认 model 为刚才配置的 data source:

"sap.ui5": {"_version": "1.1.0","rootView": {"viewName": "zui5_odata_sap_crud.view.App","type": "XML"},"dependencies": {"minUI5Version": "1.30.0","libs": {"sap.ui.core": {},"sap.m": {},"sap.ui.layout": {}}},"contentDensities": {"compact": true,"cozy": true},"models": {"i18n": {"type": "sap.ui.model.resource.ResourceModel","settings": {"bundleName": "zui5_odata_sap_crud.i18n.i18n"}},"": {"dataSource": "zempprj_srv","settings": {"metadataUrlParams": {"sap-documentation": "heading"}}}},"resources": {"css": [{"uri": "css/style.css"}]}}

此时运行程序,应该会提示输入用户名和密码,表示数据源配置成功。

界面设置

主界面

因为不打算使用 routing,直接在 root view ,即 App.view.xml 文件中设置主要的 UI 元素:

<mvc:View xmlns:html="http://www.w3.org/1999/xhtml" xmlns:mvc="sap.ui.core.mvc" xmlns="sap.m"controllerName="zui5_odata_sap_backend_crud.controller.App"><App><pages><Page title="{i18n>title}"><content><Table noDataText="No data" id="idTable" items="{path:'/EmployeeCollection'}"><items><ColumnListItem type="Navigation" press="onItemPress"><cells><Text text="{EmpId}"/><Text text="{EmpName}"/><Text text="{EmpAddr}"/></cells></ColumnListItem></items><columns><Column id="EmpIdCol"><header><Label text="Employee ID"/></header></Column><Column id="EmpNameCol"><header><Label text="Name"/></header></Column><Column id="EmpAddrCol"><header><Label text="Address"/></header></Column></columns></Table></content><footer><Bar><contentRight><Button icon="sap-icon://create" text="Create" press="onCreate"/><Button icon="sap-icon://edit" text="Edit" press="onEdit"/><Button icon="sap-icon://delete" text="Delete" press="onDelete"/></contentRight></Bar></footer></Page></pages></App>
</mvc:View>

对话框

<core:FragmentDefinition xmlns="sap.m" xmlns:core="sap.ui.core" xmlns:f="sap.ui.layout.form"><Dialog id="employeeDialog" title="Employee CRUD"><f:SimpleForm><Label text="Employee Id"/><Input id="EmpId" value="{EmpId}"/><Label text="Name"/><Input id="EmpName" value="{EmpName}"/><Label text="Address"/><Input id="EmpAddr" value="{EmpAddr}"/></f:SimpleForm><Toolbar><ToolbarSpacer/><Button id="SaveCreate" text="Save"/><Button id="SaveEdit" text="Save Edit"/><Button id="CancelButton" text="Cancel" /></Toolbar></Dialog></core:FragmentDefinition>

控制器代码

主要的代码都在 App.controller.js 中,先给出完整代码:

sap.ui.define(["sap/ui/core/mvc/Controller"
], function(Controller) {"use strict";var oModel;var sCurrentPath; // current pathvar sCurrentEmp; // cureent employeereturn Controller.extend("zui5_odata_sap_backend_crud.controller.App", {onInit: function() {oModel = this.getOwnerComponent().getModel();oModel.setUseBatch(false);this.getView().setModel(oModel);},openDialog: function() {var oView = this.getView();// Open dialogvar oEmpDialog = oView.byId("employeeDialog");if (!oEmpDialog) {oEmpDialog = sap.ui.xmlfragment(oView.getId(),"zui5_odata_sap_backend_crud.view.EmployeeDialog");oView.addDependent(oEmpDialog);}oEmpDialog.open();// Attach press event for CancelButtonvar oCancelButton = oView.byId("CancelButton");oCancelButton.attachPress(function() {oEmpDialog.close();});},// onCreate eventonCreate: function() {var oView = this.getView();this.openDialog();var oEmployeeDialog = oView.byId("employeeDialog");oEmployeeDialog.setTitle("Create Employee");oView.byId("EmpId").setEditable(true);oView.byId("SaveEdit").setVisible(false);oView.byId("SaveCreate").setVisible(true);// clearoView.byId("EmpId").setValue("");oView.byId("EmpName").setValue("");oView.byId("EmpAddr").setValue("");// commit saveoView.byId("SaveCreate").attachPress(function() {var oNewEntry = {"Mandt": "100","EmpId": "","EmpName": "","EmpAddr": ""};// populate value from formoNewEntry.EmpId = oView.byId("EmpId").getValue();oNewEntry.EmpName = oView.byId("EmpName").getValue();oNewEntry.EmpAddr = oView.byId("EmpAddr").getValue();// Commit creation operationoModel.create("/EmployeeCollection", oNewEntry, {success: function() {sap.m.MessageToast.show("Created successfully.");},error: function(oError) {window.console.log("Error", oError);}});// close dialogif (oEmployeeDialog) {oEmployeeDialog.close();}});},onEdit: function() {// no employee was selectedif (!sCurrentEmp) {sap.m.MessageToast.show("No Employee was selected.");return;}var oView = this.getView();this.openDialog();var oEmployeeDialog = oView.byId("employeeDialog");oEmployeeDialog.setTitle("Edit Employee");oView.byId("EmpId").setEditable(false);oView.byId("SaveEdit").setVisible(true);oView.byId("SaveCreate").setVisible(false);// populate fieldsoView.byId("EmpId").setValue(oModel.getProperty(sCurrentPath + "/EmpId"));oView.byId("EmpName").setValue(oModel.getProperty(sCurrentPath + "/EmpName"));oView.byId("EmpAddr").setValue(oModel.getProperty(sCurrentPath + "/EmpAddr"));// Attach save eventoView.byId("SaveEdit").attachPress(function() {var oChanges = {"Mandt": "100","EmpName": "","EmpAddr": ""};// populate value from formoChanges.EmpName = oView.byId("EmpName").getValue();oChanges.EmpAddr = oView.byId("EmpAddr").getValue();// commit creationoModel.update(sCurrentPath, oChanges, {success: function() {sap.m.MessageToast.show("Changes were saved successfully.");},error: function(oError) {window.console.log("Error", oError);}});// close dialogif (oEmployeeDialog) {oEmployeeDialog.close();}});},// onDelete eventonDelete: function() {var that = this;// no employee was selectedif (!sCurrentEmp) {sap.m.MessageToast.show("No Employee was selected.");return;}var oDeleteDialog = new sap.m.Dialog();oDeleteDialog.setTitle("Deletion");var oText = new sap.m.Label({text: "Are you sure to delete employee [" + sCurrentEmp + "]?"});oDeleteDialog.addContent(oText);oDeleteDialog.addButton(new sap.m.Button({text: "Confirm",press: function() {that.deleteEmployee();oDeleteDialog.close();}}));oDeleteDialog.open();},// deletion operationdeleteEmployee: function() {oModel.remove(sCurrentPath, {success: function() {sap.m.MessageToast.show("Deletion successful.");},error: function(oError) {window.console.log("Error", oError);}});},onItemPress: function(evt) {var oContext = evt.getSource().getBindingContext();sCurrentPath = oContext.getPath();sCurrentEmp = oContext.getProperty("EmpName");}});
});

要点说明:

Model

我们使用的是 OData Model,但是并没有任何代码来显示申明。OData Model 的声明来自 manifest.json。根据 OpenUI5 SDK,如果 DataSource 为 OData,没有指定 type,则默认的 type 为 OData v2,这正是我们想要的。

然后在 Controller 的 onInit 事件中绑定 view 和 model:

onInit: function() {oModel = this.getOwnerComponent().getModel();oModel.setUseBatch(false);this.getView().setModel(oModel);
}

新增记录 ( create )

var oNewEntry = {"Mandt": "100","EmpId": "","EmpName": "","EmpAddr": ""
};// populate value from form
oNewEntry.EmpId = oView.byId("EmpId").getValue();
oNewEntry.EmpName = oView.byId("EmpName").getValue();
oNewEntry.EmpAddr = oView.byId("EmpAddr").getValue();// Commit creation operation
oModel.create("/EmployeeCollection", oNewEntry, {success: function() {sap.m.MessageToast.show("Created successfully.");},error: function(oError) {window.console.log("Error", oError);}
});

修改记录

var oChanges = {"Mandt": "100","EmpName": "","EmpAddr": ""
};// populate value from form
oChanges.EmpName = oView.byId("EmpName").getValue();
oChanges.EmpAddr = oView.byId("EmpAddr").getValue();// commit creation
oModel.update(sCurrentPath, oChanges, {success: function() {sap.m.MessageToast.show("Changes were saved successfully.");},error: function(oError) {window.console.log("Error", oError);}
});

删除记录

deleteEmployee: function() {oModel.remove(sCurrentPath, {success: function() {sap.m.MessageToast.show("Deletion successful.");},error: function(oError) {window.console.log("Error", oError);}});
}

源码

36_01_zui5_odata_sap_backend_crud
36_02_zui5_odata_sap_backend_crud

参考资料

  • Connecting Remote Systems in SAP Web IDE Personal Edition
  • Descriptor for Applications, Components, and Libraries
  • Connect to Remote Server SAP WEB IDE local
  • Connect the SAPWebIDE On-Premise to your SAP Gateway system On-Premise
  • Create a Project based on a SAP Gateway Service ( OData ) with WebIDE On-Premise

这篇关于SAPUI5 (36) - OData Model 连接后端 SAP 系统 (下)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/681104

相关文章

java连接opcua的常见问题及解决方法

《java连接opcua的常见问题及解决方法》本文将使用EclipseMilo作为示例库,演示如何在Java中使用匿名、用户名密码以及证书加密三种方式连接到OPCUA服务器,若需要使用其他SDK,原理... 目录一、前言二、准备工作三、匿名方式连接3.1 匿名方式简介3.2 示例代码四、用户名密码方式连接4

MySQL 表的内外连接案例详解

《MySQL表的内外连接案例详解》本文给大家介绍MySQL表的内外连接,结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录表的内外连接(重点)内连接外连接表的内外连接(重点)内连接内连接实际上就是利用where子句对两种表形成的笛卡儿积进行筛选,我

Windows 系统下 Nginx 的配置步骤详解

《Windows系统下Nginx的配置步骤详解》Nginx是一款功能强大的软件,在互联网领域有广泛应用,简单来说,它就像一个聪明的交通指挥员,能让网站运行得更高效、更稳定,:本文主要介绍W... 目录一、为什么要用 Nginx二、Windows 系统下 Nginx 的配置步骤1. 下载 Nginx2. 解压

如何确定哪些软件是Mac系统自带的? Mac系统内置应用查看技巧

《如何确定哪些软件是Mac系统自带的?Mac系统内置应用查看技巧》如何确定哪些软件是Mac系统自带的?mac系统中有很多自带的应用,想要看看哪些是系统自带,该怎么查看呢?下面我们就来看看Mac系统内... 在MAC电脑上,可以使用以下方法来确定哪些软件是系统自带的:1.应用程序文件夹打开应用程序文件夹

windows系统上如何进行maven安装和配置方式

《windows系统上如何进行maven安装和配置方式》:本文主要介绍windows系统上如何进行maven安装和配置方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不... 目录1. Maven 简介2. maven的下载与安装2.1 下载 Maven2.2 Maven安装2.

Apache 高级配置实战之从连接保持到日志分析的完整指南

《Apache高级配置实战之从连接保持到日志分析的完整指南》本文带你从连接保持优化开始,一路走到访问控制和日志管理,最后用AWStats来分析网站数据,对Apache配置日志分析相关知识感兴趣的朋友... 目录Apache 高级配置实战:从连接保持到日志分析的完整指南前言 一、Apache 连接保持 - 性

使用Python实现Windows系统垃圾清理

《使用Python实现Windows系统垃圾清理》Windows自带的磁盘清理工具功能有限,无法深度清理各类垃圾文件,所以本文为大家介绍了如何使用Python+PyQt5开发一个Windows系统垃圾... 目录一、开发背景与工具概述1.1 为什么需要专业清理工具1.2 工具设计理念二、工具核心功能解析2.

Linux系统之stress-ng测压工具的使用

《Linux系统之stress-ng测压工具的使用》:本文主要介绍Linux系统之stress-ng测压工具的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、理论1.stress工具简介与安装2.语法及参数3.具体安装二、实验1.运行8 cpu, 4 fo

电脑蓝牙连不上怎么办? 5 招教你轻松修复Mac蓝牙连接问题的技巧

《电脑蓝牙连不上怎么办?5招教你轻松修复Mac蓝牙连接问题的技巧》蓝牙连接问题是一些Mac用户经常遇到的常见问题之一,在本文章中,我们将提供一些有用的提示和技巧,帮助您解决可能出现的蓝牙连接问... 蓝牙作为一种流行的无线技术,已经成为我们连接各种设备的重要工具。在 MAC 上,你可以根据自己的需求,轻松地

宝塔安装的MySQL无法连接的情况及解决方案

《宝塔安装的MySQL无法连接的情况及解决方案》宝塔面板是一款流行的服务器管理工具,其中集成的MySQL数据库有时会出现连接问题,本文详细介绍两种最常见的MySQL连接错误:“1130-Hostisn... 目录一、错误 1130:Host ‘xxx.xxx.xxx.xxx’ is not allowed