【问题标题】:OpenUI5 with Spring Boot Web - can these work together?OpenUI5 与 Spring Boot Web - 这些可以一起工作吗?
【发布时间】:2016-05-06 02:56:48
【问题描述】:

我正在尝试使用 OpenUI5 作为客户端 javascript UI 库,使用 Thymeleaf 和嵌入式 Tomcat 创建一个非常简单的 Spring Boot 1.3.3 Web 应用程序... 但我似乎无法让 OpenUI5加载 data-sap-ui-resourceroots。这应该是可能的吗?

我首先转到start.spring.io 并生成一个具有 Web 和 Thymeleaf 依赖项的 maven 项目,然后添加到具有 XML 视图的基本 OpenUI5 结构中。我遵循了以下教程:

  1. Serving Web Content with Spring MVC
  2. OpenUI5 Walkthrough - Step 4: XML Views

我最终得到了以下项目结构:

root
├── src
│    ├── main
│    │    ├── java
│    │    │   └── myPackage
│    │    │       ├── controllers
│    │    │       │   └── LandingPageController.java
│    │    │       └── Application.java
│    │    └── resources
│    │        ├── application.properties
│    │        ├── static
│    │        │   └── webapp
│    │        │       └── view
│    │        │           └── App.view.xml
│    │        └── templates
│    │            └── landingPage.html
│    └── test
│        └── java
│            └── myPackage
│                └── ApplicationTests.java
│
└── pom.xml

尽管这些教程基本上都是标准的,但为了完整起见,我将包含我的源代码:

Application.java:

package myPackage;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

LandingPageController.java:

package myPackage.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/")
public class LandingPageController {

    @RequestMapping(method=RequestMethod.GET)
    public String redirect() {
        return "landingPage";
    }
}

landingPage.html:

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta charset="utf-8" />
    <title>SAPUI5 Walkthrough</title>
    <script
     id="sap-ui-bootstrap"
     src="https://openui5.hana.ondemand.com/resources/sap-ui-core.js" 
     data-sap-ui-theme="sap_bluecrystal"
     data-sap-ui-libs="sap.m"
     data-sap-ui-compatVersion="edge"
     data-sap-ui-preload="async" 
     data-sap-ui-resourceroots='{
        "sap.ui.demo.wt": "./"
     }'>
    </script>
    <script>
     sap.ui.getCore().attachInit(function () {
         sap.ui.xmlview({
             viewName : "sap.ui.demo.wt.view.App"
          }).placeAt("content");
     });
    </script>
  </head>
  <body class="sapUiBody" id="content">
  </body>
</html>

App.view.xml:

<mvc:View
   xmlns="sap.m"
   xmlns:mvc="sap.ui.core.mvc">
   <Text text="Hello World"/>
</mvc:View>

我的 application.properties 文件是空的,而 pom.xml 正是由 Spring Initializr 生成的。

我认为问题在于landingPage.html 中的这一行,但我不知道除了“./”之外还能放什么:

data-sap-ui-resourceroots='{
        "sap.ui.demo.wt": "./"
     }

我能够让它在 OpenUI5 教程中的第 3 步工作;但是,似乎一旦我必须在 XML 文件中定义视图,OpenUI5 库就无法找到它。我不知道该把 App.view.xml 文件放在哪里,我原以为静态文件夹下的任何地方都可以......

我不确定这是否与我使用的是嵌入式 Tomcat 的事实有关,但我希望它能够使用嵌入式 Tomcat。

关于如何完成这项工作的任何建议?

【问题讨论】:

  • landingPage.html 的 URL 是什么样的?
  • 我理解 Spring MVC 的工作方式,你实际上无法通过 URL 找到landingPage.html,因为它实际上是一个视图模板而不是静态资源。基于 LandingPageController 中定义的 RequestMapping,通过导航到localhost:8080,它将触发 redirect() 方法,该方法将呈现landingPage.html 视图模板。我拥有的唯一静态文件是 OpenUI5 XML 视图,可以在 localhost:8080/webapp/view/App.view.xml 找到

标签: spring-mvc spring-boot sapui5


【解决方案1】:

我不熟悉 Spring 或任何提到的 Java 框架,但我可以尝试在 resourceRoots 方面为您提供帮助。

通过该设置,您可以告诉 OpenUI5 框架在哪里可以找到某些资源。

"sap.ui.demo.wt": "./" 的设置意味着以sap.ui.demo.wt 开头的所有内容都可以在与当前页面/文件相同的目录中找到。任何进一步的.&lt;something&gt; 表示具有该名称的子文件夹。

您可以使用例如:
"sap.ui.demo.wt": "/resources/static/webapp"
相对于您的 html 文件或绝对定义 resourceRoots 在这种情况下,您的名称为 sap.ui.demo.wt.view.App 的视图将在 /resources/static/webapp/view/App.view.xml 中找到(或至少查找)。

【讨论】:

  • 感谢@hirse 的帮助。当我运行应用程序时,我可以在localhost:8080/webapp/view/App.view.xml 找到视图。所以在resourceRoots中,我尝试将其设置为"sap.ui.demo.wt": "/webapp",我什至尝试了"sap.ui.demo.wt": "/webapp/view"。我也采纳了您的建议并尝试了"sap.ui.demo.wt": "/resources/static/webapp"。这些都不起作用......我不确定它是如何使用 resourceRoots 来搜索服务器的,但我想知道嵌入式 Tomcat 是否在某种程度上阻碍了这一点......
  • 另外(不太理想)有没有办法明确指定资源,而不是通过定义根来找到它们? sap.ui.registerResource("/webapp/view/App.view.xml") 之类的东西?
  • @sadiq,您可以使用浏览器开发者工具查看请求的路径并尝试进行相应调整。
猜你喜欢
  • 2019-08-05
  • 2018-12-24
  • 1970-01-01
  • 2017-08-31
  • 2019-12-21
  • 2018-07-16
  • 2015-09-03
  • 2017-03-23
相关资源
最近更新 更多