SAP CAP篇六:为CAP添加Fiori Elements程序(3)

本文目录

  • 本系列之前的文章
  • 本篇之前的运行效果
  • 修改`pom.xml`
    • 添加新的Dependency
    • 更新plugin
  • 添加新的Controller
    • 创建`index`目录
    • 添加`UiIndexContentProviderFactory.java`
  • 添加`resources`
  • 更新 `application.yaml`
  • 运行结果
  • 更新`UiIndexContentProviderFactory `
  • 代码库 (Gitcode)

本系列之前的文章

本系列之前的文章:

SAP CAP篇一:快速创建一个Service,基于Java的实现
SAP CAP篇二:为Service加上数据库支持
SAP CAP篇三:定义Model
SAP CAP篇四:为CAP添加Fiori Elements程序(1)
SAP CAP篇五:为CAP添加Fiori Elements程序(1)

本篇之前的运行效果

在完成 SAP CAP篇五:为CAP添加Fiori Elements程序(1) 的基础之上,执行:

mvn spring-boot:run

打开浏览器:
SAP CAP篇六:为CAP添加Fiori Elements程序(3)_第1张图片

修改pom.xml

继续增强程序。首先,修改在srv文件夹中的pom.xml

添加新的Dependency

添加新的Dependency:

<dependency>
	<groupId>com.sap.cdsgroupId>
	<artifactId>cds-adapter-apiartifactId>
dependency>

更新plugin

buildplugins下,修改下面这个plugin添加workingDirectory

<plugin>
	<groupId>org.springframework.bootgroupId>
	<artifactId>spring-boot-maven-pluginartifactId>
	<version>${spring.boot.version}version>
	<configuration>
		<skip>falseskip>
		
		<workingDirectory>..workingDirectory>
	configuration>
	<executions>
		<execution>
			<id>repackageid>
			<goals>
				<goal>repackagegoal>
			goals>
			<configuration>
				<classifier>execclassifier>
			configuration>
		execution>
	executions>
plugin>

添加新的Controller

创建index目录

Application.java所在的文件夹,添加index目录。

添加UiIndexContentProviderFactory.java

添加UiIndexContentProviderFactory.java文件夹:

package customer.demo_service.index;

import java.io.PrintWriter;
import com.sap.cds.adapter.IndexContentProvider;
import com.sap.cds.adapter.IndexContentProviderFactory;

/**
 * Explicitly adds links to UI resources provided by this application to the index page
 */
public class UiIndexContentProviderFactory implements IndexContentProviderFactory {

	@Override
	public IndexContentProvider create() {
		return new UiIndexContentProvider();
	}

	@Override
	public boolean isEnabled() {
		return true;
	}

	private static class UiIndexContentProvider implements IndexContentProvider {

		private static final String ENDPOINT_START = "" +
		"                
    \n"; private static final String ENDPOINT = "" + "
  • \n" + " %s\n" + "
  • \n"
    ; private static final String ENDPOINT_END = "" + "
\n"
; @Override public String getSectionTitle() { return "UI endpoints"; } @Override public void writeContent(PrintWriter writer, String contextPath) { writer.print(ENDPOINT_START); writer.printf(ENDPOINT, contextPath + "/manage-books/webapp/index.html?sap-ui-xx-viewCache=false", "Manage Books"); writer.print(ENDPOINT_END); } } }

添加resources

resources文件夹下,执行如下步骤:

  • 添加文件夹META-INF
  • META-INF文件夹下添加文件夹services
  • serivces文件夹下添加文件com.sap.cds.adapter.IndexContentProviderFactory
  • 在刚刚创建的文件中,添加一行纯文本:customer.demo_service.index.UiIndexContentProviderFactory

更新 application.yaml

更新resource文件夹下的application.yaml

让Spring Boot支持Resource的Static Folder:

spring.web.resources.static-locations: "file:./app/"

运行结果

这时,再运行如下命令编译程序:

mvn clean install

再运行命令打开程序:

mvn spring-boot:run

页面效果——这时UI endpoints出现了:
SAP CAP篇六:为CAP添加Fiori Elements程序(3)_第2张图片

点击Manage Books链接,可以打开Fiori Elements

SAP CAP篇六:为CAP添加Fiori Elements程序(3)_第3张图片

更新UiIndexContentProviderFactory

上面只添加了manage-books程序,还要添加browse-books

@Override
public void writeContent(PrintWriter writer, String contextPath) {
	writer.print(ENDPOINT_START);
	writer.printf(ENDPOINT, contextPath + "/manage-books/webapp/index.html?sap-ui-xx-viewCache=false", "Manage Books");
	writer.printf(ENDPOINT, contextPath + "/browse-books/webapp/index.html?sap-ui-xx-viewCache=false", "Browse Books");
	writer. Print(ENDPOINT_END);
}

代码库 (Gitcode)

与本文配套的代码

参考README.md关于各个branch的详细介绍。

本篇对应的branch2_withfe

你可能感兴趣的:(ABAP/SAP,Web,Programming,Spring,Boot,SAP,CDS,SAP,CAP,Fiori,Element,Spring,Boot,Cloud,Native,云原生)