gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 05:56:34 25 4
gpt4 key购买 nike

我正在使用 Spark-java 编写一个小型网络应用程序。我使用 thymeleaf 作为模板引擎,intellij 是我一直使用的 IDE。我面临的问题是变量没有按应有的方式在网页中呈现。

我已将 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)
);
}

我的 html 代码包含以下代码片段

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

但网页上不显示文字。 Intellij 说无法解析 todaysdate

最佳答案

我认为如果删除以下代码,它就会显示。

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

因为,ThymeleafTemplateEngine 使用“/templates”下的文件。如果指定了 staticFiles.location,则优先。因此,输出的是未模板化的index.html。

关于java - 如何使用thymeleaf + Spark-java Web框架在网页中显示消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56008328/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com