You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot+Thymeleaf项目Controller传值至页面不显示问题求助

Troubleshooting Thymeleaf Value Display Issues in Spring Boot 2.0.1.RELEASE

Hey there, let's work through this problem step by step—since you're using Spring Boot 2.0.1.RELEASE, there are several common spots to check when values from your Controller aren't showing up in your Thymeleaf templates:

1. Verify You Have the Complete Thymeleaf Dependency

From the pom.xml snippet you shared, I don't see the Thymeleaf starter dependency listed. Spring Boot won't auto-configure the Thymeleaf engine unless you include this:

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

This dependency pulls in all necessary Thymeleaf libraries and manages the version automatically to match your Spring Boot parent, so you don't need to specify a version yourself.

2. Check Your Controller's Value-Passing Logic

Make sure you're using the correct methods to pass values to the template. Here are the two most reliable approaches:

  • Using Model to add attributes:
@GetMapping("/dashboard")
public String loadDashboard(Model model) {
    // Add your value to the model
    model.addAttribute("userName", "Vito");
    // Return the template name (without .html extension)
    return "dashboard";
}
  • Using ModelAndView:
@GetMapping("/dashboard")
public ModelAndView loadDashboard() {
    ModelAndView mav = new ModelAndView("dashboard");
    mav.addObject("userName", "Vito");
    return mav;
}

Double-check that the string you return matches the exact name of your HTML file in the templates directory (e.g., returning dashboard maps to src/main/resources/templates/dashboard.html—it's case-sensitive!).

3. Confirm Thymeleaf Template Syntax is Correct

First, ensure your HTML root tag includes the Thymeleaf namespace—this is required for the engine to recognize Thymeleaf attributes:

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

Then, use the proper attribute to render your value. You can't just write ${userName} directly in text; you need to use th:text (for plain text) or th:utext (for HTML content):

<p>Welcome back, <span th:text="${userName}">Guest</span></p>

The text inside the span ("Guest") is a fallback that will show if Thymeleaf isn't working—if you see this fallback instead of your value, that's a sign the template isn't being processed by Thymeleaf.

4. Validate Template File Location and Naming

Spring Boot looks for Thymeleaf templates in src/main/resources/templates/ by default. If your HTML file is anywhere else (like src/main/resources/static/), Thymeleaf won't process it. Also, make sure the filename matches the string returned from your Controller exactly.

5. Enable Debug Logs to Dig Deeper

If you're still stuck, turn on debug logging to see what's happening behind the scenes. Add these lines to your application.properties:

logging.level.org.springframework.web=DEBUG
logging.level.org.thymeleaf=DEBUG

You'll see logs showing if your Controller is passing attributes correctly, and if Thymeleaf is loading the template file successfully.

6. Disable Template Cache in Development

Thymeleaf caches templates by default, which means changes to your HTML won't show up immediately when you refresh the page. Disable caching in development with this property:

spring.thymeleaf.cache=false

内容的提问来源于stack exchange,提问作者Vito Lipari

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:25:09