Spring Boot应用中Messages资源信息在Thymeleaf中的显示错误问题

引言: Thymeleaf是Spring Boot中使用的一种后台的模板技术,在Spring Boot中碰到了在页面上无法访问messages中信息的问题,本文将描述其分析过程以及如何解决该问题。

1.  环境介绍

    Spring Boot 1.4.0,  Thymeleaf的模板技术后台使用。

    使用基于messages的存放信息,比如中文情况下使用messages_zh_CN.properties来存放信息。

2.  问题以及错误描述

    在Thymeleaf页面中使用#{message_key}来直接从messages中读取信息。 测试中页面报出来的错误信息如下:

??menu.top.client.service.text_zh_CN??
   其中使用的messages.properties定义了如下信息:
menu.top.client.service.text=\u8054\u7CFB\u5BA2\u670D
  但是却无法正确的显示出来,那问题出在哪里呢?

3.  页面代码分析

   页面的代码如下:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
    <head>
        <title>Hello World!</title>
    </head>
    <body>
        <h1 th:inline="text">Hello.v.2</h1>
        <p th:text="${hello}"></p>
        <p th:text="#{menu.top.client.service.text}">test message property</p>
    </body>
</html>
  分析器中<p>中使用的th:text应该没有问题。

4.   messages文件的位置

    如果页面是正确的话,查看messages的靖是否正确, 指定basename的路径,经过分析其为正确的。

spring.messages.basename=i18n/messages
5.  打开thymeleaf的调试模式 
spring.thymeleaf.cache=false
  关闭其cache设置,可以直接使用其修改的template,不再缓存。

6.   重新启动之后, 可以正常显示

   在写blog的过程中,重启之后,可以正常显示了, 问题忽然之久就消失了,难道是缓存的问题,不得而知,期待下一次碰到之后,补齐本文剩余的原因内容。


  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
好的,下面我将为你展示如何实现一个基于Spring Boot、Mybatis-Plus、ThymeleafBootstrap的留言板。 1. 创建Spring Boot项目 首先,我们需要创建一个Spring Boot项目。你可以直接在IDE创建,也可以使用Spring Boot CLI。这里我使用IDEA创建一个Maven项目。 2. 配置pom.xml 在pom.xml文件添加以下依赖: ``` <dependencies> <!-- Spring Boot Starter --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Thymeleaf --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!-- MyBatis-Plus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.1.1</version> </dependency> <!-- MySQL Connector --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.21</version> </dependency> <!-- Bootstrap --> <dependency> <groupId>org.webjars</groupId> <artifactId>bootstrap</artifactId> <version>4.5.0</version> </dependency> <!-- Webjars Locator --> <dependency> <groupId>org.webjars</groupId> <artifactId>webjars-locator</artifactId> <version>0.40</version> </dependency> </dependencies> ``` 这里使用了Spring BootThymeleaf、Mybatis-Plus、MySQL Connector、Bootstrap以及Webjars Locator。 3. 配置数据库 我们需要在application.properties文件配置数据库连接信息。 ``` spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver ``` 这里使用MySQL作为数据库,数据库名为test,用户名和密码均为root。 4. 创建留言实体类 我们需要创建一个留言实体类,用于映射数据库的留言表。 ``` public class Message { private Long id; private String username; private String content; private Date createTime; // 省略getter和setter方法 } ``` 5. 创建Mapper 我们需要创建一个Mapper接口,用于操作留言表。 ``` @Mapper public interface MessageMapper extends BaseMapper<Message> { List<Message> selectAll(); } ``` 这里使用了Mybatis-Plus提供的BaseMapper作为父接口,简化了Mapper的编写。 6. 创建Controller 我们需要创建一个Controller,用于处理请求。 ``` @Controller public class MessageController { @Autowired private MessageMapper messageMapper; @GetMapping("/") public String index(Model model) { List<Message> messages = messageMapper.selectAll(); model.addAttribute("messages", messages); return "index"; } @PostMapping("/") public String addMessage(Message message) { message.setCreateTime(new Date()); messageMapper.insert(message); return "redirect:/"; } } ``` 这里使用了@Autowired注解注入了MessageMapper。在index方法,我们查询了所有留言,并将其存入Model,然后返回index视图。在addMessage方法,我们添加了一条留言,并重定向到首页。 7. 创建视图 我们需要创建一个视图,用于显示留言列表和添加留言。 ``` <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org" xmlns:webjars="http://www.thymeleaf.org/extras/webjar"> <head> <title>留言板</title> <!-- Bootstrap --> <link rel="stylesheet" href="/webjars/bootstrap/4.5.0/css/bootstrap.min.css" /> </head> <body> <div class="container"> <h1>留言板</h1> <hr/> <form method="post"> <div class="form-group"> <label for="username">用户名:</label> <input type="text" class="form-control" id="username" name="username" required="required" /> </div> <div class="form-group"> <label for="content">留言内容:</label> <textarea class="form-control" id="content" name="content" required="required"></textarea> </div> <button type="submit" class="btn btn-primary">提交留言</button> </form> <hr/> <table class="table table-striped"> <thead> <tr> <th>用户名</th> <th>留言内容</th> <th>留言时间</th> </tr> </thead> <tbody> <tr th:each="message : ${messages}"> <td th:text="${message.username}"></td> <td th:text="${message.content}"></td> <td th:text="${#dates.format(message.createTime, 'yyyy-MM-dd HH:mm:ss')}"></td> </tr> </tbody> </table> </div> <!-- Bootstrap --> <script src="/webjars/bootstrap/4.5.0/js/bootstrap.min.js"></script> </body> </html> ``` 这里使用了Thymeleaf模板引擎,使用了Bootstrap样式。在表格,我们使用了th:each指令循环遍历留言列表,使用了#dates.format方法格式化留言时间。 8. 运行项目 现在,我们可以运行项目并访问http://localhost:8080来查看留言板了。 以上就是一个简单的Spring Boot、Mybatis-Plus、ThymeleafBootstrap留言板的实现过程。希望对你有帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值