模板解析期间发生错误(模板:“类路径资源 [templates/addWunsch.html]”)[关闭]
Posted
技术标签:
【中文标题】模板解析期间发生错误(模板:“类路径资源 [templates/addWunsch.html]”)[关闭]【英文标题】:An error happened during template parsing (template: "class path resource [templates/addWunsch.html]") [closed] 【发布时间】:2020-04-29 10:37:54 【问题描述】:我正在尝试打开我的 html 页面 addWunsch.html 但总是出错。我一直在搜索格式和名称错误,但没有找到。
现在每当我尝试访问 localhost/addWunsch 时,我都会收到错误消息
异常:org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常是 org.thymeleaf.exceptions.TemplateInputException: 模板解析时出错(模板:“类路径资源 [templates/addWunsch.html]”)
状态:500
这是我的 addWunsch.html:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Wunsch hinzufügen</title>
<link rel="stylesheet" type="text/css" th:href="@styles.css"/>
</head>
<body>
<h1>Teile uns deinen Vorschlag mit:</h1>
<form th:action="@/addWunsch" th:object="$PizzaWunschForm" method="POST">
Pizzaname:
<input type="text" th:field="*pizzaWunschName" />
<br/>
Pizzabeschreibung:
<input type="text" th:field="*pizzaWunschBeschreibung" />
<br/>
<input type="submit" value="Create" />
</form>
<br/>
<!-- Check if errorMessage is not null and not empty -->
<div th:if="$errorMessage" th:utext="$errorMessage" style="color:red;font-style:italic;"></div>
</body>
</html>
这是我的控制器:
package de.frauas.projekt.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import de.frauas.projekt.form.PizzaWunschForm;
import de.frauas.projekt.model.PizzaWunsch;
@Controller
public class WunschController
private static List<PizzaWunsch> pizzaWunschListe = new ArrayList<PizzaWunsch>();
@Value("$welcome.message")
private String message;
@Value("$error.message")
private String errorMessage;
@RequestMapping(value = "/wunsch" , method = RequestMethod.GET)
public String showWunschIndex(Model model)
model.addAttribute("message", message);
return "wunschIndex";
@RequestMapping(value = "/listWunsch" , method = RequestMethod.GET)
public String showListWunsch(Model model)
model.addAttribute("pizzaWunschListe", pizzaWunschListe);
return "listWunsch";
@RequestMapping(value = "/addWunsch" , method = RequestMethod.GET)
public String showAddWunsch(Model model)
PizzaWunschForm pizzaWunschForm = new PizzaWunschForm();
model.addAttribute("pizzaWunschForm", pizzaWunschForm);
return "addWunsch";
@RequestMapping(value = "/addWunsch" , method = RequestMethod.POST)
public String saveWunschPizza(Model model, @ModelAttribute("PizzaWunschForm") PizzaWunschForm pizzaWunschForm)
String pizzaWunschName = pizzaWunschForm.getPizzaWunschName();
String pizzaWunschBeschreibung = pizzaWunschForm.getPizzaWunschBeschreibung();
if (pizzaWunschName != null && pizzaWunschName.length() > 0 && pizzaWunschBeschreibung != null
&& pizzaWunschBeschreibung.length() > 0)
PizzaWunsch newPizzaWunsch = new PizzaWunsch(pizzaWunschName, pizzaWunschBeschreibung);
pizzaWunschListe.add(newPizzaWunsch);
return "redirect:/listWunsch";
model.addAttribute("errorMessage", errorMessage);
return "addWunsch";
【问题讨论】:
【参考方案1】:请更改
<form th:action="@/addWunsch" th:object="$PizzaWunschForm" method="POST">
到
<form th:action="@/addWunsch" th:object="$pizzaWunschForm" method="POST">
...因为您已向 Model
添加了一个由 lowerCamelCase 键表示的属性。区分大小写。
【讨论】:
以上是关于模板解析期间发生错误(模板:“类路径资源 [templates/addWunsch.html]”)[关闭]的主要内容,如果未能解决你的问题,请参考以下文章
Vue.js 3 s-s-r - 客户端水合期间缺少模板或渲染功能
模板渲染期间的 Django 错误。找不到带有参数“(”,)'和关键字参数''的'name'的反向