Thymeleaf 不显示来自弹簧控制器的值 [英] Thymeleaf not displaying values from spring controller

查看:30
本文介绍了Thymeleaf 不显示来自弹簧控制器的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是第一次练习 thymeleaf 模板引擎.我已经按照教程等进行操作,但我不知道我哪里出错了.

I am practising thymeleaf template engine for the first time. I have followed the tutorial and so on but I have no idea where I am going wrong.

我的控制器:

public String mainPage(Model model){
    model.addAttribute("data", "Hello Thymeleaf");  
    return "main";
}

我的html如下:

<!DOCTYPE html >
<html xmlns:th="http://www.thymeleaf.org">

<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>

<body>
   <h1>th:text="${data}"</h1>
</body>
</html>

当我点击 localhost 它显示 th:text="${data}" 而不是 Hello Thymeleaf

When I hit localhost it displays th:text="${data}" instead of Hello Thymeleaf

<h1>"${data}"</h1>

也不行.视图解析器配置必须正确,因为它将 main 解析为 main.html.我正在使用 spring4 SpringTemplateEngine 和 spring4 thymeleaf 视图解析器.

doesn't work either. View resolver config must be correct as it resolves main to main.html. I am using spring4 SpringTemplateEngine and spring4 thymeleaf view resolver.

提前致谢

推荐答案

你必须使用th:text

 <h1 th:text="${data}"></h1>

或者如果您不想使用 th:text 属性,那么您必须使用 th:inline="text" 并使百里香叶呈现标签内的上下文.但请确保将变量放在 [[]]

or if you don't want to use the th:text attribute, then you have to use th:inline="text" and make the thymeleaf render the context inside the tag. But make sure you put the variable inside [[ and ]]

 <h1 th:inline="text">[[${data}]]</h1>

这篇关于Thymeleaf 不显示来自弹簧控制器的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆