无法使用Spark Java Template Engine呈现css和js文件

时间:2016-08-29 13:51:58

标签: freemarker spark-java

我是Spark Java框架的新手,我尝试使用Freemarker模板引擎渲染我的html文件。但是我无法渲染我的css和js文件。

我的项目结构如下:

./pom.xml
./src/main/java/Spark.java
./src/main/resources/css/bootstrap.css
./src/main/resources/js/bootstrap.min.js
./src/main/resources/js/app.js
./src/main/resources/templates/search.ftl

主要方法如下:

public static void main(String[] args) {

        BasicConfigurator.configure();

        staticFileLocation("/css");
        staticFileLocation("/Content/Images");
        staticFileLocation("/fonts");
        staticFileLocation("/js");

        final Configuration configuration = new Configuration();
        configuration.setClassForTemplateLoading(Spark.class, "/");

        get("/searchview", (request, response) -> {

            StringWriter writer = new StringWriter();

            try {
                Template searchTemplate = configuration.getTemplate("templates/search.ftl");

                searchTemplate.process(null, writer);
            } catch (Exception e) {
                halt(500);
            }

            return writer;
        });
}

现在当我输入url:localhost:4567 / searchview时,我的HTML页面会被渲染,但css和js不会。

在我的控制台中它说:INFO spark.http.matching.MatcherFilter - 请求的路径[/css/bootstrap.min.css]尚未在Spark中映射

我错过了什么?

1 个答案:

答案 0 :(得分:0)

您之后的staticFileLocation次调用将覆盖您之前设置的静态文件位置。在public下创建一个名为src/main/resources的文件夹,然后将cssfontsjs和其他任何静态文件夹放在那里。最后按staticFileLocation("/public")设置静态文件位置。

相关问题