从处理表单提交指南(Spring入门指南)获取代码错误

问题描述 投票:0回答:1

我正在关注this入门指南并跟踪错误:

org.thymeleaf.exceptions.TemplateProcessingException:执行处理器'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor'时出错(模板:“index” - 第10行,第31行)“

代码与指南中显示的完全相同。我知道这与Thymeleaf表格标签有关,所以我也检查了Thymeleaf documentation,但无法弄清楚我的代码有什么问题。

这是代码:

的index.html

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Getting Started: Handling Form Submission</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
  <h1>Form</h1>
  <form action="#" th:action="@{/greeting}" th:object="${greeting}" method="post">
    <p>Id: <input type="text" th:field="*{id}" /></p>
    <p>Message: <input type="text" th:field="*{content}" /></p>
    <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
  </form>
</body>
</html>

greeting controller.Java

package com.example;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;

@Controller
public class GreetingController {

    @GetMapping("/greeting")
    public String greetingForm(Model model) {
        model.addAttribute("greeting", new Greeting());
        return "greeting";
    }

    @PostMapping("/greeting")
    public String greetingSubmit(@ModelAttribute Greeting greeting) {
        return "result";
    }
}

greeting.Java

package com.example;

public class Greeting {

    private long id;
    private String content;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

}

我很感激任何建议。

java spring thymeleaf
1个回答
0
投票

所以教程说你要创建2个文件,result.htmlgreeting.html。你创造了index.html。问题是你没有将模板页面绑定到Spring控制器,它与tuturial中的greeting.html绑定。

因此解决方案只是在greetingForm()方法中将问候语的回报更改为索引:

@GetMapping("/greeting")
public String greetingForm(Model model) {
    model.addAttribute("greeting", new Greeting());
    return "index"; // <- changed line
}

希望这会对你有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.