我正在尝试打开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";
    }
}

最佳答案

请更换

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




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


...因为您已将 CamelCase键表示的属性添加到Model中。区分大小写。

关于java - 模板解析期间发生错误(模板:“类路径资源[templates/addWunsch.html]”),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59704366/

10-09 07:06