使用JPA + RESTful Web Service + SAPUI5来创建Web应用

在之前的例子学习了如何使用JPA,如何通过Apache olingo来将JPA数据库自动转化为RESTful Web Service。

学习了如何使用SAPUI5,现在我来把它们整合起来。


开发环境:

开发环境是:Eclipse Juno, MySQL 5.5,olingo 1.2,EclipseLink 2.4, Tomcat 7, SAPUI5 1.18


这里数据层就直接使用上一个练习完成的jpa2项目,它实现了一个对后台数据库表employee操作的RESTful Web Service,

url: http://localhost:8080/jpa2/Employee.svc/


1.安装SAP提供的Eclipse SAPUI5插件。

https://tools.hana.ondemand.com


2.创建一个SAPUI5的Web Project,名字叫sapui5

3.由于我的服务是local的,所以需要使用proxy来访问,修改一下web.xml

添加:

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1.     <servlet>  
  2.         <servlet-name>SimpleProxyServlet</servlet-name>  
  3.         <servlet-class>com.sap.ui5.proxy.SimpleProxyServlet</servlet-class>  
  4.     </servlet>  
  5.     <servlet-mapping>  
  6.         <servlet-name>SimpleProxyServlet</servlet-name>  
  7.         <url-pattern>/proxy/*</url-pattern>  
  8.     </servlet-mapping>  
  9. <context-param>  
  10.         <param-name>com.sap.ui5.proxy.REMOTE_LOCATION</param-name>  
  11.         <param-value>http://localhost:8080</param-value>  
  12.     </context-param>  

4.修改index.html文件:

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <!DOCTYPE HTML>  
  2. <html>  
  3. <head>  
  4. <meta http-equiv="X-UA-Compatible" content="IE=edge">  
  5. <meta http-equiv='Content-Type' content='text/html;charset=UTF-8' />  
  6.   
  7. <script src="resources/sap-ui-core.js" id="sap-ui-bootstrap"  
  8.     data-sap-ui-libs="sap.ui.commons, sap.ui.table"  
  9.     data-sap-ui-theme="sap_bluecrystal">  
  10.       
  11. </script>  
  12. <script>  
  13.     //One place for defining service URLs - either in the index.html file,   
  14.     //or in one separate .js file which needs to be included.  
  15.     //The application is responsible for exchanging the URLs before checking in and   
  16.     //after checking out to SAPUI5 Repository;  
  17.     //or using a helper function getServiceUrl for which also the application   
  18.     //is responsible.  
  19.   
  20.     //var serviceUrl = "/mypath/myservice";       //url when running on the ABAP system  
  21.     //var serviceUrl = "proxy/mypath/myservice";  //url when running locally in Eclipse  
  22.   
  23.     var serviceUrl = getServiceUrl("/jpa2/Employee.svc/");  
  24.   
  25.     function getServiceUrl(sServiceUrl) {  
  26.         //for local testing prefix with proxy  
  27.         //if you and your team use a special host name or IP like 127.0.0.1 for localhost please adapt the if statement below   
  28.         if (window.location.hostname == "localhost") {  
  29.             return "proxy" + sServiceUrl;  
  30.         } else {  
  31.             return sServiceUrl;  
  32.         }  
  33.     }  
  34. </script>  
  35.   
  36. <script>  
  37.     // create the DataTable control  
  38.     var oTable = new sap.ui.table.Table({  
  39.         editable : true  
  40.     });  
  41.   
  42.     // define the Table columns  
  43.     var oControl = new sap.ui.commons.TextView({  
  44.         text : "{Id}"  
  45.     }); // short binding notation  
  46.     oTable.addColumn(new sap.ui.table.Column({  
  47.         label : new sap.ui.commons.Label({  
  48.             text : "ID"  
  49.         }),  
  50.         template : oControl,  
  51.         sortProperty : "id",  
  52.         filterProperty : "id",  
  53.         width : "100px"  
  54.     }));  
  55.   
  56.     // define the Table columns  
  57.     var oControl = new sap.ui.commons.TextView({  
  58.         text : "{FirstName}"  
  59.     }); // short binding notation  
  60.     oTable.addColumn(new sap.ui.table.Column({  
  61.         label : new sap.ui.commons.Label({  
  62.             text : "First Name"  
  63.         }),  
  64.         template : oControl,  
  65.         sortProperty : "firstName",  
  66.         filterProperty : "firstName",  
  67.         width : "100px"  
  68.     }));  
  69.   
  70.     // define the Table columns  
  71.     var oControl = new sap.ui.commons.TextView({  
  72.         text : "{LastName}"  
  73.     }); // short binding notation  
  74.     oTable.addColumn(new sap.ui.table.Column({  
  75.         label : new sap.ui.commons.Label({  
  76.             text : "Last Name"  
  77.         }),  
  78.         template : oControl,  
  79.         sortProperty : "lastName",  
  80.         filterProperty : "lastName",  
  81.         width : "100px"  
  82.     }));  
  83.   
  84.     var oModel = new sap.ui.model.odata.ODataModel(serviceUrl);  
  85.   
  86.     oTable.setModel(oModel);  
  87.     oTable.bindRows("/Employees");  
  88.   
  89.     // finally place the Table into the UI  
  90.     oTable.placeAt("content");  
  91. </script>  
  92.   
  93. </head>  
  94. <body class="sapUiBody" role="application">  
  95.     <div id="content"></div>  
  96. </body>  
  97. </html>  

这里主要就是创建一个table组件,然后创建一个odata model,然后它们绑定一下就行了。


5.部署项目在Tomcat上,运行:

6.小结:在得到了RESTful的service以后,进行web和移动开发以后就非常方便了,而使用SAPUI5则变得更方便了,不仅界面漂亮,还可以大大提高开发效率。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值