【Springboot采坑日记】Springboot+thymeleaf集成i18n的配置过程及注意事项

本文介绍了如何在Springboot项目中集成Thymeleaf和i18n,包括导入Thymeleaf依赖,配置Thymeleaf与Springboot的国际化支持,创建解析器和拦截器,以及在前端HTML中使用。同时,文章强调了配置过程中需要注意的细节,如basename的命名规范、MessageUtils的默认值设定和避免key冲突的方法。
摘要由CSDN通过智能技术生成

【Springboot采坑日记】Springboot+thymeleaf集成i18n过程及注意事项

Springboot+thymeleaf+i18n的配置方式

thymeleaf集成

导入thymeleaf依赖:

		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
            <version>2.3.3.RELEASE</version>
        </dependency>

配置thymeleaf的yml文件

spring:
  thymeleaf:
    cache: false # 将thymeleaf缓存关闭
    encoding: UTF-8

然后就可以将值传入model中,由前端html直接展示
Controller

@Controller
public class TestController {
   

    @RequestMapping("test")
    public String indexHandler(Model model, HttpServletRequest request, HttpSession session){
   
        model.addAttribute("welcome","hello world");
        model.addAttribute("student",new Test("zhangsan",80));
        model.addAttribute("gender","male");
        List<Test> students = new ArrayList<>();
        students.add(new Test("张三",13));
        students.add(new Test("李四",25));
        students.add(new Test("赵三",60));
        model.addAttribute("students",students);
        Map<String,Object> map = new HashMap<>();
        map.put("stu7",new Test("田七",27));
        map.put("stu8",new Test("刘八",28));
        map.put("stu9",new Test("郑九",29));
        model.addAttribute("map",map);
        model.addAttribute("attrName","score");
        model.addAttribute("attrValue",99);
        model.addAttribute("welcome1","<h2>Thymeleaf,<br>I'm learning.</h2>");
        model.addAttribute("photo","email.png");
        model.addAttribute("elementId","reddiv");
        model.addAttribute("bgColor","red");
        model.addAttribute("isClose",false);
        model.addAttribute("school",null);
        List<String> cities = new ArrayList<>();
        model.addAttribute("cities",cities);
        request.setAttribute("req","reqValue");
        session.setAttribute("ses","sesValue");
        session.getServletContext().setAttribute("app","appValue");
        int[] nums = {
   1,2,3,4,5,6};
        model.addAttribute("nums",nums);
        model.addAttribute("today",new Date());
        model.addAttribute("cardId","684515202008258475");
        //这里test表示thymeleaf所对应的test.html,不用写扩展名
        return "test";
    }
 }

HTML

<!DOCTYPE html>
<html lang="en" xmlns:th="https://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Hello world</title>
</head>

<!--请求路径:http://localhost:8888/test?name=zhangsan-->
<body>
<!--展示文本 类似jsp ${}-->
    <p th:text="${welcome}">这里将显示数据,但这些文字不显示</p>
    <div th:text="${welcome}">这里将显示数据,但这些文字不显示</div>
    <span th:text="${welcome}">这里将显示数据,但这些文字不显示</span>
    <hr>

<!--展示类-->
    <p th:text="${student}" id="student">aaaa</p>
    <p th:text="${student.name}">vvvv</p>
    <p th:text="${student.age}"
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

镜云兮

打赏的都是爷~求各位爷可怜可怜

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值