You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Spring Boot用Thymeleaf模板时出现WhiteLabel 500错误求助

Troubleshooting Thymeleaf TemplateInputException in Your Spring Boot Taco Cloud App

Hey there, let's work through this 500 Internal Server Error you're facing with Thymeleaf. That TemplateInputException usually flags issues with template parsing or missing/incorrect data passed from your backend to the view. Let's break down the most likely fixes:

1. Fix the Template Filename Space (Critical First Check!)

Looking at your error message:

org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/design.html ]")

Notice the trailing space in design.html ? Thymeleaf is trying to load a file with that space, which almost certainly doesn't exist. Double-check:

  • Your actual template file in src/main/resources/templates/ is named design.html (no trailing space)
  • In your controller, you're returning the view name correctly without extra spaces. For example, use return "design"; (Spring Boot automatically looks for .html files in the templates folder, so you don't need to include the extension at all)

2. Verify Backend Data is Properly Passed to the Model

Your template references several variables (${design}, ${wrap}, ${protein}, ${cheese}, ${veggies}, ${sauce}) that must be added to the Model in your controller. Make sure your controller method looks something like this:

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@GetMapping("/design")
public String showDesignForm(Model model) {
    // Add the empty Taco object for form binding
    model.addAttribute("design", new Taco());
    
    // Fetch ingredients grouped by type and add to model
    model.addAttribute("wrap", ingredientRepository.findByType("WRAP"));
    model.addAttribute("protein", ingredientRepository.findByType("PROTEIN"));
    model.addAttribute("cheese", ingredientRepository.findByType("CHEESE"));
    model.addAttribute("veggies", ingredientRepository.findByType("VEGGIES"));
    model.addAttribute("sauce", ingredientRepository.findByType("SAUCE"));
    
    return "design";
}

Also, confirm your Taco class uses Lombok's @Data annotation to generate getters/setters (required for Thymeleaf form binding):

import lombok.Data;
import java.util.List;

@Data
public class Taco {
    private String name;
    private List<Ingredient> ingredients;
}

3. Check for Thymeleaf Syntax Quirks

  • The th:field="*{ingredients}" for checkboxes is valid for binding to a collection, but ensure Taco's ingredients field is a mutable collection (like List<Ingredient>) with a setter (provided by @Data).
  • If any of the ingredient lists (${wrap}, etc.) are null (not just empty), Thymeleaf will throw an error. Add null checks in your controller, or initialize empty collections if no ingredients are found:
    model.addAttribute("wrap", Optional.ofNullable(ingredientRepository.findByType("WRAP")).orElse(List.of()));
    

4. Validate Template File Location & Case

  • Confirm design.html is directly under src/main/resources/templates/ (no subfolders unless you reference them in the view name).
  • On Linux/macOS, filenames are case-sensitive—make sure your controller returns design (lowercase) if the file is design.html, not Design.html.

Quick Test to Isolate the Issue

To narrow down the problem, temporarily simplify your design.html to just:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Taco Cloud</title>
</head>
<body>
<h1>Design your taco!</h1>
</body>
</html>

If this renders without errors, the issue is with the dynamic content in your original template. Add back sections one by one (starting with the design form object) to find which part is causing the parsing failure.


Your Original design.html Code for Reference

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Taco Cloud</title>
</head>
<body>
<h1>Design your taco!</h1>
<form method="POST" th:object="${design}">
<div class="grid">
<div class="ingredient-group" id="wraps">
<h3>Designate your wrap:</h3>
<div th:each="ingredient : ${wrap}">
<input th:field="*{ingredients}" type="checkbox" th:value="${ingredient.id}"/>
<span th:text="${ingredient.name}">INGREDIENT</span><br/>
</div>
</div>
<div class="ingredient-group" id="proteins">
<h3>Pick your protein:</h3>
<div th:each="ingredient : ${protein}">
<input th:field="*{ingredients}" type="checkbox" th:value="${ingredient.id}"/>
<span th:text="${ingredient.name}">INGREDIENT</span><br/>
</div>
</div>
<div class="ingredient-group" id="cheeses">
<h3>Choose your cheese:</h3>
<div th:each="ingredient : ${cheese}">
<input th:field="*{ingredients}" type="checkbox" th:value="${ingredient.id}"/>
<span th:text="${ingredient.name}">INGREDIENT</span><br/>
</div>
</div>
<div class="ingredient-group" id="veggies">
<h3>Determine your veggies:</h3>
<div th:each="ingredient : ${veggies}">
<input th:field="*{ingredients}" type="checkbox" th:value="${ingredient.id}"/>
<span th:text="${ingredient.name}">INGREDIENT</span><br/>
</div>
</div>
<div class="ingredient-group" id="sauces">
<h3>Select your sauce:</h3>
<div th:each="ingredient : ${sauce}">
<input th:field="*{ingredients}" type="checkbox" th:value="${ingredient.id}"/>
<span th:text="${ingredient.name}">INGREDIENT</span><br/>
</div>
</div>
</div>
<div>
<h3>Name your taco creation:</h3>
<input type="text" th:field="*{name}"/>
<br/>
<button>Submit Your Taco</button>
</div>
</form>
</body>
</html>

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

火山引擎 最新活动