简体   繁体   English

如何使用thymeleaf + spark-java web框架在网页中显示消息?

[英]How to display message in webpage using thymeleaf + spark-java web framework?

I am writing a small web app using spark-java. 我正在使用spark-java编写一个小型Web应用程序。 I have used thymeleaf as template engine and intellij is the IDE I have been using. 我使用百万美元作为模板引擎,intellij是我一直使用的IDE。 The problem I am facing is that variable is not rendering in webpage as it should have. 我面临的问题是变量没有在网页中呈现,因为它应该具有。

I have passed todaysdate attribute to the template engine 我已将todaysdate属性传递给模板引擎

public void init() {
    Spark.staticFiles.location("/templates");
    get("/", (req, res) -> {
        Map<String, Object> model = new HashMap<String, Object>();
        DateModel dateModel = getDate();
        String date = "Monday 6, May 2019";
        model.put("todaysdate", date);
        return render(model, "index");
    });
}

private String render(Map<String, Object> model, String pageName){
    return new ThymeleafTemplateEngine().render(
            new ModelAndView(model, pageName)
    );
}

My html code contains the following code snipped 我的html代码包含以下代码剪切

<div class="col-2 date" id="currentdate" th:text="${todaysdate}"></div>

But the text is not displayed on the webpage. 但文本未显示在网页上。 Intellij is saying cannot resolve todaysdate . Intellij说无法解决todaysdate

I think that it will be displayed if the following code is removed. 我认为如果删除以下代码将显示它。

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

Because, ThymeleafTemplateEngine uses files under "/templates". 因为,ThymeleafTemplateEngine使用“/ templates”下的文件。 If staticFiles.location is specified, that takes precedence. 如果指定了staticFiles.location,则优先。 Therefore, index.html which is not templated is output. 因此,输出未模板化的index.html。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM