Thymeleaf模板无法解析Controller对象及模板找不到问题排查求助
Thymeleaf模板无法解析Controller对象及模板找不到问题排查求助
我最近跟着《Spring in Action(第6版)》做Taco项目时遇到了两个棘手的问题,想请教各位大佬帮忙排查下:
- HTML视图文件里的
taco、wrap、ingredient、id等变量都被IDE红线下划线标注,总共提示32个错误,显示无法解析这些对象/属性。 - 项目运行后访问页面时,控制台抛出如下错误:
2023-03-10T21:28:22.305+06:00 ERROR 16760 --- [nio-8080-exec-1] org.thymeleaf.TemplateEngine : [THYMELEAF][http-nio-8080-exec-1] Exception processing template "design": Error resolving template [design], template might not exist or might not be accessible by any of the configured Template Resolvers
我已经确认添加了Thymeleaf相关配置,并且类路径是正确的,但还是出现这些问题,有没有小伙伴遇到过类似情况?或者能帮忙分析下可能的原因吗?
我的HTML模板代码:
<h1>Design your taco!</h1> <form method="POST" th:object="${taco}"> <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>
我的DesignController代码:
package com.example.demo.web; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.*; import lombok.extern.slf4j.Slf4j; import com.example.demo.Ingredient; import com.example.demo.Ingredient.Type; import com.example.demo.Taco; import com.example.demo.TacoOrder; @Slf4j @Controller @RequestMapping("/design") @SessionAttributes("tacoOrder") public class DesignController { @ModelAttribute public void addIngredientsToModel(Model model) { List<Ingredient> ingredients = Arrays.asList( new Ingredient("FLTO", "Flour Tortilla", Type.WRAP), new Ingredient("COTO", "Corn Tortilla", Type.WRAP), new Ingredient("GRBF", "Ground Beef", Type.PROTEIN), new Ingredient("CARN", "Carnitas", Type.PROTEIN), new Ingredient("TMTO", "Diced Tomatoes", Type.VEGGIES), new Ingredient("LETC", "Lettuce", Type.VEGGIES), new Ingredient("CHED", "Cheddar", Type.CHEESE), new Ingredient("JACK", "Monterrey Jack", Type.CHEESE), new Ingredient("SLSA", "Salsa", Type.SAUCE), new Ingredient("SRCR", "Sour Cream", Type.SAUCE) ); Type[] types = Ingredient.Type.values(); for (Type type : types) { model.addAttribute(type.toString().toLowerCase(), filterByType(ingredients, type)); } } @ModelAttribute(name = "tacoOrder") public TacoOrder order() { return new TacoOrder(); } @ModelAttribute(name = "taco") public Taco taco() { return new Taco(); } @GetMapping public String showDesignForm() { return "design"; } private Iterable<Ingredient> filterByType( List<Ingredient> ingredients, Type type) { return ingredients .stream() .filter(x -> x.getType().equals(type)) .collect(Collectors.toList()); } }
备注:内容来源于stack exchange,提问作者Sanzhar B.
相关产品推荐
相关产品推荐

