简体   繁体   中英

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

I am writing a small web app using spark-java. I have used thymeleaf as template engine and intellij is the IDE I have been using. 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

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

<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 .

I think that it will be displayed if the following code is removed.

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

Because, ThymeleafTemplateEngine uses files under "/templates". If staticFiles.location is specified, that takes precedence. Therefore, index.html which is not templated is output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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