在html文件中, 首先导入thymeleaf的名称空间
<html lang="en" xmlns:th="http://www.thymeleaf.org">
然后先感受一下thymeleaf的语法--he:
先在controller中配置:
1 @Controller 2 public class Hellocontroller { 3 4 @ResponseBody 5 @RequestMapping("/success") 6 public String success(Map<String,Object> map){ 7 map.put("hello", "你好你好~~~"); 8 return "success"; 9 } 10 }
html文件代码如下:
1 <!DOCTYPE html> 2 <html lang="en" xmlns:th="http://www.thymeleaf.org"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>success</title> 6 </head> 7 <body> 8 <h2>成功!!!</h2> 9 <!--设置div的文本--> 10 <div th:text="${hello}">这是显示的欢迎信息</div> 11 12 </body> 13 </html>
启动项目后浏览器访问: localhost:8080/success, 将返回:
可以看见controller中的数据传到了HTML中, 可以通过这种方式将后台的数据在前端显示