在Maven驱动的Vaadin

在Maven驱动的Vaadin

本文介绍了在Maven驱动的Vaadin 14 Web应用程序中启动Web应用程序时,将Java代码加载的数据或配置文件放在何处的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在由 Vaadin入门页面,Maven POM文件过程自动创建了许多文件夹.

在哪里放置要在我的Web应用启动时加载并解析的数据文件或配置文件?我可以将文件放在哪个文件夹下?我应该创建更多的嵌套文件夹吗?并通过什么文件路径名找到并加载这样的文本(XML,JSON,CSV等)文件?

解决方案

文件位置

您将它们放在src/main/resources中,因为这也是Vaadin应用程序外部的约定.

Vaadin为后来添加到jar中的东西添加了其他几个资源根,这些东西便于vaadin查找(例如,在META-INF/resources/...下).

resources仍然最终位于jar的根中,或者被构建工具正确地类路径化",并且可以安全地通过应用程序中的类加载器加载非类.

正在打开文件

您可以通过调用 Class::getResourceAsStream ,返回 InputStream .注意前导斜杠字符.

InputStream inputStream = this.getClass().getResourceAsStream( "/myfile.txt" ) ;

In a Vaadin 14 web app project created by the "Plain Java Servlet" flavor of the Vaadin Starter page, there are numerous folders automatically created by the Maven POM file process.

Where is the place to put a data file or configuration file that I will load and parse when my web app launches? Under what folder do I put my files? Should I create further nested folders? And by what file path name do I find and load such a text (XML, JSON, CSV, etc.) file?

解决方案

Location of file

You put them in src/main/resources as it is the convention also outside of a Vaadin application.

Vaadin adds several other resource roots for things that later end up in places in the jar, that are conventient for vaadin to find (e.g. under META-INF/resources/...).

resources still ends up in the root of the jar or get properly "classpathed" by the build-tools and is safe to load non-classes via the classloader in your application.

Opening file

You can open your text file from there by calling Class::getResourceAsStream, returning an InputStream. Note the leading slash character.

InputStream inputStream = this.getClass().getResourceAsStream( "/myfile.txt" ) ;

这篇关于在Maven驱动的Vaadin 14 Web应用程序中启动Web应用程序时,将Java代码加载的数据或配置文件放在何处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 20:55