springboot-thymeleaf提示Undefined attribute name (th:text)

在eclipse中使用thymeleaf时,会出现“Undefined attribute name (th:text).”提示。解决办法一是在html标签中书写,二是把要取值的标签写在block标签里。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在eclipse中用到thymeleaf时会提示“Undefined attribute name (th:text).”

解决办法 
1、在html标签中写

<html xmlns:th="http://www.thymeleaf.org">

2、把要取值的标签写在block标签里

<th:block xmlns:th="http://www.thymeleaf.org">
    <div th:text="${'Here we go Thymeleaf!'}"></div>
</th:block>

 

### 如何在Spring Boot中使用Thymeleaf模板引擎 要在Spring Boot项目中使用Thymeleaf模板引擎,可以按照以下方法实现: #### Maven依赖配置 为了使Spring Boot能够支持Thymeleaf,需要在`pom.xml`文件中引入对应的Maven依赖项。以下是具体的依赖声明[^2]: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> ``` #### 设置Thymeleaf版本 如果希望指定特定版本的Thymeleaf,可以在项目的`properties`部分进行设置。通常情况下,默认使用的版本已经满足大多数需求。 #### 配置属性 可以通过修改`application.properties`或`application.yml`文件中的参数来自定义Thymeleaf的行为。例如,在开发环境中关闭缓存以实时预览更改[^4]: ```properties spring.thymeleaf.cache=false spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html ``` 这些配置分别指定了HTML模板的位置前缀、后缀以及是否启用缓存功能。 #### 创建Controller类 创建一个Spring MVC Controller用于处理HTTP请求并将数据传递给视图层。下面是一个简单的例子[^1]: ```java import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @Controller public class HelloController { @GetMapping("/") public String index(Model model){ model.addAttribute("message", "Hello, Thymeleaf!"); return "index"; } } ``` 此代码片段展示了如何向模型添加消息并通过返回字符串指向名为`index.html`的Thymeleaf模板。 #### 编写Thymeleaf模板 最后一步是在资源目录下的`templates`文件夹里编写实际的HTML页面。这里给出一段基本示例[^3]: ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>Home Page</title> </head> <body> <h1 th:text="${message}">Default Message</h1> </body> </html> ``` 上述HTML文档利用了Thymeleaf特有的语法`${}`表达式绑定服务器端传来的变量值。 总结来说,Spring Boot提供了非常便捷的方式让开发者快速上手Thymeleaf这一强大的模板工具[^5]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值