【问题标题】:An error happened during template parsing (template: "class path resource [templates/addWunsch.html]") [closed]模板解析期间发生错误(模板:“类路径资源 [templates/addWunsch.html]”)[关闭]
【发布时间】: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";
    }
}

【问题讨论】:

    标签: java html spring spring-boot thymeleaf


    【解决方案1】:

    请更改

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-09
      • 2020-12-01
      • 2022-10-17
      • 2021-02-15
      • 2019-03-17
      • 1970-01-01
      • 2018-11-30
      • 1970-01-01
      相关资源
      最近更新 更多