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

相关文章

C#连接SQL server数据库命令的基本步骤

《C#连接SQLserver数据库命令的基本步骤》文章讲解了连接SQLServer数据库的步骤,包括引入命名空间、构建连接字符串、使用SqlConnection和SqlCommand执行SQL操作,... 目录建议配合使用:如何下载和安装SQL server数据库-CSDN博客1. 引入必要的命名空间2.

golang程序打包成脚本部署到Linux系统方式

《golang程序打包成脚本部署到Linux系统方式》Golang程序通过本地编译(设置GOOS为linux生成无后缀二进制文件),上传至Linux服务器后赋权执行,使用nohup命令实现后台运行,完... 目录本地编译golang程序上传Golang二进制文件到linux服务器总结本地编译Golang程序

Linux系统性能检测命令详解

《Linux系统性能检测命令详解》本文介绍了Linux系统常用的监控命令(如top、vmstat、iostat、htop等)及其参数功能,涵盖进程状态、内存使用、磁盘I/O、系统负载等多维度资源监控,... 目录toppsuptimevmstatIOStatiotopslabtophtopdstatnmon

Java通过驱动包(jar包)连接MySQL数据库的步骤总结及验证方式

《Java通过驱动包(jar包)连接MySQL数据库的步骤总结及验证方式》本文详细介绍如何使用Java通过JDBC连接MySQL数据库,包括下载驱动、配置Eclipse环境、检测数据库连接等关键步骤,... 目录一、下载驱动包二、放jar包三、检测数据库连接JavaJava 如何使用 JDBC 连接 mys

Qt使用QSqlDatabase连接MySQL实现增删改查功能

《Qt使用QSqlDatabase连接MySQL实现增删改查功能》这篇文章主要为大家详细介绍了Qt如何使用QSqlDatabase连接MySQL实现增删改查功能,文中的示例代码讲解详细,感兴趣的小伙伴... 目录一、创建数据表二、连接mysql数据库三、封装成一个完整的轻量级 ORM 风格类3.1 表结构

MySQL中的表连接原理分析

《MySQL中的表连接原理分析》:本文主要介绍MySQL中的表连接原理分析,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、背景2、环境3、表连接原理【1】驱动表和被驱动表【2】内连接【3】外连接【4编程】嵌套循环连接【5】join buffer4、总结1、背景

linux重启命令有哪些? 7个实用的Linux系统重启命令汇总

《linux重启命令有哪些?7个实用的Linux系统重启命令汇总》Linux系统提供了多种重启命令,常用的包括shutdown-r、reboot、init6等,不同命令适用于不同场景,本文将详细... 在管理和维护 linux 服务器时,完成系统更新、故障排查或日常维护后,重启系统往往是必不可少的步骤。本文

SpringBoot连接Redis集群教程

《SpringBoot连接Redis集群教程》:本文主要介绍SpringBoot连接Redis集群教程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1. 依赖2. 修改配置文件3. 创建RedisClusterConfig4. 测试总结1. 依赖 <de

Mac系统下卸载JAVA和JDK的步骤

《Mac系统下卸载JAVA和JDK的步骤》JDK是Java语言的软件开发工具包,它提供了开发和运行Java应用程序所需的工具、库和资源,:本文主要介绍Mac系统下卸载JAVA和JDK的相关资料,需... 目录1. 卸载系统自带的 Java 版本检查当前 Java 版本通过命令卸载系统 Java2. 卸载自定

基于Python实现一个简单的题库与在线考试系统

《基于Python实现一个简单的题库与在线考试系统》在当今信息化教育时代,在线学习与考试系统已成为教育技术领域的重要组成部分,本文就来介绍一下如何使用Python和PyQt5框架开发一个名为白泽题库系... 目录概述功能特点界面展示系统架构设计类结构图Excel题库填写格式模板题库题目填写格式表核心数据结构