其他分享
首页 > 其他分享> > Thymeleaf基础语法和使用

Thymeleaf基础语法和使用

作者:互联网

1.引用

修改标签
<html xmlns:th="http://www.thymeleaf.org">

以下语法需要在th:内部使用才能正常解析,否则会原样显示

2.输出内容

th:text="" 显示文本信息,不解析
<p th:text="<strong>qqq</strong>"></p>
结果:<strong>qqq</strong>

th:utext="" 显示文本信息,如果有标签会解析
<p th:utext="<strong>qqq</strong>"></p>
结果:qqq

3.基本表达式:

变量表达式   ${ }  用于获取上下文参数值,例

 @RequestMapping("/")
    public String test(Model model){
        model.addAttribute("name","123456");
        return "index";
    }



//index.html

//<h2 th:text="${name}"></h2>

选择表达式  *{ }  用于选择当前对象的一个属性值。当前对象通过   th:object="${  }"  来设置  

<div th:object="${object}">
    name:<span th:text="*{name}"/>'
    age:<span th:text="*{age}"/>
</div>

信息表达式   #{ }   用于获取全局公用的一些静态文本信息,以便维护使用,这些信息一般放在properties文件中。一般搭配th:text=" "使用

文件名称必须中messages开始,messages1也不行,messages_zh_CN可以用,小项目就直接用messages作为名称就好了。th:text="#{ key }"

<p th:text="#{test.qqq}"></p>
<p th:utext="#{test.qqq}"></p>

4.引入URL

通过@{......}引入url

默认访问的根路径为src/main/resources/static

如访问src/main/resources/static/css/index.css文件,则

<link rel="stylesheet"  th:href="@{css/index.css}"/>

也可以访问绝对路径

<link rel="stylesheet"  th:href="@{https://www.nihao.com/css/index.css}"/>

如果直接使用href

<link rel="stylesheet"  href="@{https://www.nihao.com/css/index.css}"/>

则路径不会解析,导致出错,必须使用th:href=" "

5.访问WebContext对象中的属性

Thymeleaf模板适配了WebContext中的参数请求,

 

 

 

 

 

 

 

 

 

标签:index,messages,text,基础,语法,Thymeleaf,qqq,th,表达式
来源: https://blog.csdn.net/qq_43319748/article/details/117464221