Spark 框架和相对路径
Spark Framework and relative path
我正在使用 Spark framework 创建网络应用程序,并且一直在浏览他们网站上的教程。目前,我正在研究他们的模板部分,但是我似乎无法让项目识别出我的 css 和我的模板位于我的资源文件夹中。
我正在使用 Netbeans 和 Maven 来管理我的依赖项。
谁能帮我弄清楚如何在此环境中适当地设置我的亲戚 paths/create 我的项目文件夹?我是 Maven 和 Spark 的新手,所以请放轻松。
静态文件:
如果您的资源目录如下所示:
resources
└───public
├───css
│ style.css
├───html
│ hello.html
└───templates
template.ftl
您可以使用 staticFiles.location("/public")
。这将使 /public
成为根 staticFiles 目录。
然后您可以像这样访问 hello.html
:http://{host}:{port}/html/hello.html
如果你想在文件系统上使用外部位置,你可以使用 staticFiles.externalLocation(...)
,其工作方式与上面几乎相同。
Note: staticFiles.externalLocation(...)
can be set to your project's resources directory, which means that the files will be automatically refreshed (useful for development)
中找到更深入的解释
正在配置您的模板引擎:
如果您已经设置了 staticFiles 位置,但 spark 仍然无法找到您的模板,请试试这个。
Note: These examples are for the FreeMarker engine, though they should apply to other engines with minor tweaking.
查看 examples 后,似乎默认情况下,new FreemarkerEngine()
会在 spark/template/freemarker
中查找模板,而 不会 您的静态文件位置。
你有两个选择:
1:将所有模板移动到该目录
或
2:配置自己的引擎,在定义路由时改为传
FreeMarkerEngine freemarker = new FreeMarkerEngine();
Configuration config = new Configuration();
config.setTemplateLoader(
new ClassTemplateLoader(YOUR_CLASS.class, "/templatedir"));
freemarker.setConfiguration(config);
我正在使用 Spark framework 创建网络应用程序,并且一直在浏览他们网站上的教程。目前,我正在研究他们的模板部分,但是我似乎无法让项目识别出我的 css 和我的模板位于我的资源文件夹中。
我正在使用 Netbeans 和 Maven 来管理我的依赖项。
谁能帮我弄清楚如何在此环境中适当地设置我的亲戚 paths/create 我的项目文件夹?我是 Maven 和 Spark 的新手,所以请放轻松。
静态文件:
如果您的资源目录如下所示:
resources
└───public
├───css
│ style.css
├───html
│ hello.html
└───templates
template.ftl
您可以使用 staticFiles.location("/public")
。这将使 /public
成为根 staticFiles 目录。
然后您可以像这样访问 hello.html
:http://{host}:{port}/html/hello.html
如果你想在文件系统上使用外部位置,你可以使用 staticFiles.externalLocation(...)
,其工作方式与上面几乎相同。
中找到更深入的解释Note:
staticFiles.externalLocation(...)
can be set to your project's resources directory, which means that the files will be automatically refreshed (useful for development)
正在配置您的模板引擎:
如果您已经设置了 staticFiles 位置,但 spark 仍然无法找到您的模板,请试试这个。
Note: These examples are for the FreeMarker engine, though they should apply to other engines with minor tweaking.
查看 examples 后,似乎默认情况下,new FreemarkerEngine()
会在 spark/template/freemarker
中查找模板,而 不会 您的静态文件位置。
你有两个选择:
1:将所有模板移动到该目录
或
2:配置自己的引擎,在定义路由时改为传
FreeMarkerEngine freemarker = new FreeMarkerEngine();
Configuration config = new Configuration();
config.setTemplateLoader(
new ClassTemplateLoader(YOUR_CLASS.class, "/templatedir"));
freemarker.setConfiguration(config);