Thymeleaf 使用#和$,以及使用#碰见??one.url_zh_CN??

1、#是用来获取配置文件中的值的。

首先需要配置.properties

在resources中创建i18n/messages文件夹

我创建了one.properties

配置文件中配置 (spring.messages.basename=i18n/messages/one)

注意后面的one必须写,他表示前缀(one_xxxx.properties)

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<p>读取配置文件信息#{}</p>
<p th:text="#{one.url}"></p>
</body>
</html>

成功显示出来了。。。。。。。

2、$本质上就是从Context中获取值

@Controller
@RequestMapping("/test")
public class Test1 {

    @GetMapping("/one")
    public String one(ModelMap mmap){
        User user = new User();
        user.setId(110);
        user.setName("测试$");
        mmap.addAttribute("user",user);
        return "one";
    }
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<p>读取Context信息${}</p>
<p th:text="${user.name}"></p>
</body>
</html>


版权声明:本文为xcc_2269861428原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。