java - 模板解析时发生错误(模板: "class path resource [templates/addWunsch.html]")

标签 java html spring spring-boot thymeleaf

我试图打开我的 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>

这是我的 Controller :

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";
    }
}

最佳答案

请更改

<form th:action="@{/addWunsch}" th:object="${PizzaWunschForm}" method="POST">

<form th:action="@{/addWunsch}" th:object="${pizzaWunschForm}" method="POST">

...因为您已向 Model 添加了一个由 lowerCamelCase 键表示的属性。区分大小写。

关于java - 模板解析时发生错误(模板: "class path resource [templates/addWunsch.html]"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59704366/

相关文章:

html - iPad 上的应用程序缓存不会缓存网站数据或页面

spring - 如何以功能方式在@RefreshScope 中注册 spring bean?

spring - 在 MongoDB 支持的 Spring Data REST 存储库中使用自定义 ID

java - 更改突出显示文本java的字体样式

jquery - 在 IE 中点击 <a href> 标签跳转,jquery 附加到 a href

java - 根据软件许可证标志更改 JAAS 角色

html - 如何使flex从底部显示元素

java - Tapestry 5 和具有相同接口(interface)的 Spring bean

java - 无法将图像图标加载到 Netbeans 中的按钮

java - 如何配置Hibernate进行开发测试