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.sql.SQLTransientConnectionException连接超时异常原因及解决方案

《java.sql.SQLTransientConnectionException连接超时异常原因及解决方案》:本文主要介绍java.sql.SQLTransientConnectionExcep... 目录一、引言二、异常信息分析三、可能的原因3.1 连接池配置不合理3.2 数据库负载过高3.3 连接泄漏

JWT + 拦截器实现无状态登录系统

《JWT+拦截器实现无状态登录系统》JWT(JSONWebToken)提供了一种无状态的解决方案:用户登录后,服务器返回一个Token,后续请求携带该Token即可完成身份验证,无需服务器存储会话... 目录✅ 引言 一、JWT 是什么? 二、技术选型 三、项目结构 四、核心代码实现4.1 添加依赖(pom

基于Python实现自动化邮件发送系统的完整指南

《基于Python实现自动化邮件发送系统的完整指南》在现代软件开发和自动化流程中,邮件通知是一个常见且实用的功能,无论是用于发送报告、告警信息还是用户提醒,通过Python实现自动化的邮件发送功能都能... 目录一、前言:二、项目概述三、配置文件 `.env` 解析四、代码结构解析1. 导入模块2. 加载环

linux系统上安装JDK8全过程

《linux系统上安装JDK8全过程》文章介绍安装JDK的必要性及Linux下JDK8的安装步骤,包括卸载旧版本、下载解压、配置环境变量等,强调开发需JDK,运行可选JRE,现JDK已集成JRE... 目录为什么要安装jdk?1.查看linux系统是否有自带的jdk:2.下载jdk压缩包2.解压3.配置环境

Mac电脑如何通过 IntelliJ IDEA 远程连接 MySQL

《Mac电脑如何通过IntelliJIDEA远程连接MySQL》本文详解Mac通过IntelliJIDEA远程连接MySQL的步骤,本文通过图文并茂的形式给大家介绍的非常详细,感兴趣的朋友跟... 目录MAC电脑通过 IntelliJ IDEA 远程连接 mysql 的详细教程一、前缀条件确认二、打开 ID

Go语言连接MySQL数据库执行基本的增删改查

《Go语言连接MySQL数据库执行基本的增删改查》在后端开发中,MySQL是最常用的关系型数据库之一,本文主要为大家详细介绍了如何使用Go连接MySQL数据库并执行基本的增删改查吧... 目录Go语言连接mysql数据库准备工作安装 MySQL 驱动代码实现运行结果注意事项Go语言执行基本的增删改查准备工作

python连接sqlite3简单用法完整例子

《python连接sqlite3简单用法完整例子》SQLite3是一个内置的Python模块,可以通过Python的标准库轻松地使用,无需进行额外安装和配置,:本文主要介绍python连接sqli... 目录1. 连接到数据库2. 创建游标对象3. 创建表4. 插入数据5. 查询数据6. 更新数据7. 删除

Linux查询服务器系统版本号的多种方法

《Linux查询服务器系统版本号的多种方法》在Linux系统管理和维护工作中,了解当前操作系统的版本信息是最基础也是最重要的操作之一,系统版本不仅关系到软件兼容性、安全更新策略,还直接影响到故障排查和... 目录一、引言:系统版本查询的重要性二、基础命令解析:cat /etc/Centos-release详

更改linux系统的默认Python版本方式

《更改linux系统的默认Python版本方式》通过删除原Python软链接并创建指向python3.6的新链接,可切换系统默认Python版本,需注意版本冲突、环境混乱及维护问题,建议使用pyenv... 目录更改系统的默认python版本软链接软链接的特点创建软链接的命令使用场景注意事项总结更改系统的默

在 Spring Boot 中连接 MySQL 数据库的详细步骤

《在SpringBoot中连接MySQL数据库的详细步骤》本文介绍了SpringBoot连接MySQL数据库的流程,添加依赖、配置连接信息、创建实体类与仓库接口,通过自动配置实现数据库操作,... 目录一、添加依赖二、配置数据库连接三、创建实体类四、创建仓库接口五、创建服务类六、创建控制器七、运行应用程序八