eclipse Spark Web Framework 的静态文件放在哪里?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28125886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 22:21:46  来源:igfitidea点击:

Where to put static files for Spark Web Framework?

javaeclipsemavenspark-java

提问by creatsin

Where do I put files when trying to serve static files with the Spark web framework?

尝试使用 Spark Web 框架提供静态文件时,我应该将文件放在哪里?

I haven't been able to find anything online - I'm beginning to suspect I don't understand anything about class paths, relative paths etc. for an Eclipse and Java project.

我在网上找不到任何东西 - 我开始怀疑我对 Eclipse 和 Java 项目的类路径、相对路径等一无所知。

This paragraphabout static files in Spark refers to /public, but I have no idea where that would be. Using windows, Eclipse Luna and my project is converted to use Maven.

这一段关于 Spark 中的静态文件指的是/public,但我不知道那会在哪里。使用windows,Eclipse Luna 和我的项目转换为使用Maven。

I've tried looking at the code on GitHub, but I'm a little out of my depth trying to find it.

我试过查看 GitHub 上的代码,但我在尝试找到它时有点超出我的深度。

回答by Pablo Matias Gomez

First you have to tell Spark where to search for the static files like this:

首先,您必须告诉 Spark 在哪里搜索静态文件,如下所示:

Spark.staticFiles.location("/public");

In Spark versions prior to 2.5, you should use:

在 2.5 之前的 Spark 版本中,您应该使用:

Spark.staticFileLocation("/public");

Then your project should have a publicfolder under the resources folder like this

那么你的项目应该public在资源文件夹下有一个这样的文件夹

/src/main/resources/public/style.css

/src/main/resources/public/style.css

For example I added a style.cssfile there, so you should then access it like this:

例如,我在style.css那里添加了一个文件,因此您应该像这样访问它:

http://localhost:4567/style.css

http://localhost:4567/style.css



If you want to serve a non-classpath folder, then you should use

如果要提供非类路径文件夹,则应使用

Spark.staticFiles.externalLocation("/path/to/dir");

In Spark versions prior to 2.5, you should use:

在 2.5 之前的 Spark 版本中,您应该使用:

Spark.externalStaticFileLocation("/path/to/dir");

回答by Dherik

I put my style sheets below my static content as follows:

我将样式表放在静态内容下方,如下所示:

staticFileLocation( "/web" );
/web/
  |-- index.html
  +-- styles/
        +
        +--- default.css

And the index.html

和 index.html

... <link href="styles/default.css" rel="stylesheet"   type="text/css" />

I also have other generated HTML pages as with freemarker. They just collect the path:

我还有其他生成的 HTML 页面,就像 freemarker 一样。他们只是收集路径:

  • /styles/default.css, or
  • localhost:8081/styles/default.css
  • /styles/default.css,或
  • 本地主机:8081/styles/default.css

Shows the CSS way index gets it.

显示索引获取它的 CSS 方式。

Source: https://groups.google.com/d/msg/sparkjava/5vMuK_5GEBU/vh_jHra75u0J

来源:https: //groups.google.com/d/msg/sparkjava/5vMuK_5GEBU/vh_jHra75u0J

回答by Laercio Metzner

  1. Right click your project on Eclipse, select create New -> Package. Give the new package a name, etc.

  2. Put your static resources under that package, so we can be sure they're under your classpath.

  3. In your Main class colde, call staticFileLocation("yourpackagename/");
  1. 在 Eclipse 上右键单击您的项目,选择 create New -> Package。为新包命名等。

  2. 将您的静态资源放在该包下,这样我们就可以确保它们在您的类路径下。

  3. 在你的主类冷中,调用 staticFileLocation("yourpackagename/");

回答by Moses

  1. Place your public directory into src/main/resources
  2. Replace Spark.staticFileLocation("/public");to Spark.staticFileLocation("public");
  1. 将您的公共目录放入 src/main/resources
  2. 替换Spark.staticFileLocation("/public");Spark.staticFileLocation("public");