繁体   English   中英

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

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

我正在使用spark-java编写一个小型Web应用程序。 我使用百万美元作为模板引擎,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。

暂无
暂无

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

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