@PostMapping 状态 500 错误 TemplateProcessingException 和 IllegalStatementException
@PostMapping status 500 Error TemplateProcessingException and IllegalStatementException
我正在尝试使用 Spring Boot 创建一个简单的页面,它只显示用户输入。但是当我转到 @PostMapping 路径时,它会给我一个状态 500 错误,但有以下例外:
org.thymeleaf.exceptions.TemplateProcessingException:处理器执行期间出错 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor'(模板:“greeting.html”- 第 12 行,第 31 栏)
2021-10-18 19:53:24.476 错误 9332 --- [nio-8080-exec-4] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] throw exception [Request processing failed;嵌套异常是 org.thymeleaf.exceptions.TemplateProcessingException:执行处理器 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' 期间出错(模板:“greeting.html” - 第 12 行,第 31 列)],根本原因是
java.lang.IllegalStateException:bean 名称 'greeting' 的 BindingResult 和普通目标对象都不能用作请求属性
这是我的模型:
package com.example.springplay;
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;
}
}
这是我的控制器:
package com.example.springplay;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
@Controller
public class MainController {
// @RequestMapping(value="/greeting", method = RequestMethod.GET)
@GetMapping("/greeting")
public String greeting(@RequestParam(name="name", required=false, defaultValue="World") String name, Model model) {
model.addAttribute("name", name);
return "greeting.html";
}
@PostMapping("/greeting")
public String greetingSubmit(@ModelAttribute Greeting greeting, Model model) {
model.addAttribute("greeting", greeting);
return "result.html";
}
}
这是我的 HTML 页面:
greeting.html
<!DOCTYPE HTML>
<html xmlns:th="https://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>
<p th:text="${name}"></p>
<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>-->
<button type="submit">Submit Your Post</button>
</form>
</body>
</html>
index.html
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p>Get your greeting <a th:href="@{/greeting}">here</a></p>
</body>
</html>
result.html
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>Result</h1>
<p th:text="'id: ' + ${greeting.id}" />
<p th:text="'content: ' + ${greeting.content}" />
<a href="/greeting">Submit another message</a>
</body>
</html>
编辑:我通过将参数“Greeting greeting”添加到 public String greeting() 来修复它,所以它就像
@GetMapping("/greeting")
public String greeting(@RequestParam(name="name", required=false, defaultValue="World") String name, Greeting greeting, Model model) {
model.addAttribute("name", name);
return "greeting.html";
}
虽然我不知道为什么会这样,希望有人能解释一下
你能试试吗:
@GetMapping("/greeting")
public String greeting(@RequestParam(name="name", required=false, defaultValue="World") String name, Greeting greeting, Model model) {
model.addAttribute("name", name);
model.addAttribute("greeting", new Greeting());
return "greeting.html";
}
我正在尝试使用 Spring Boot 创建一个简单的页面,它只显示用户输入。但是当我转到 @PostMapping 路径时,它会给我一个状态 500 错误,但有以下例外:
org.thymeleaf.exceptions.TemplateProcessingException:处理器执行期间出错 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor'(模板:“greeting.html”- 第 12 行,第 31 栏)
2021-10-18 19:53:24.476 错误 9332 --- [nio-8080-exec-4] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] throw exception [Request processing failed;嵌套异常是 org.thymeleaf.exceptions.TemplateProcessingException:执行处理器 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' 期间出错(模板:“greeting.html” - 第 12 行,第 31 列)],根本原因是
java.lang.IllegalStateException:bean 名称 'greeting' 的 BindingResult 和普通目标对象都不能用作请求属性
这是我的模型:
package com.example.springplay;
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;
}
}
这是我的控制器:
package com.example.springplay;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
@Controller
public class MainController {
// @RequestMapping(value="/greeting", method = RequestMethod.GET)
@GetMapping("/greeting")
public String greeting(@RequestParam(name="name", required=false, defaultValue="World") String name, Model model) {
model.addAttribute("name", name);
return "greeting.html";
}
@PostMapping("/greeting")
public String greetingSubmit(@ModelAttribute Greeting greeting, Model model) {
model.addAttribute("greeting", greeting);
return "result.html";
}
}
这是我的 HTML 页面: greeting.html
<!DOCTYPE HTML>
<html xmlns:th="https://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>
<p th:text="${name}"></p>
<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>-->
<button type="submit">Submit Your Post</button>
</form>
</body>
</html>
index.html
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p>Get your greeting <a th:href="@{/greeting}">here</a></p>
</body>
</html>
result.html
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>Result</h1>
<p th:text="'id: ' + ${greeting.id}" />
<p th:text="'content: ' + ${greeting.content}" />
<a href="/greeting">Submit another message</a>
</body>
</html>
编辑:我通过将参数“Greeting greeting”添加到 public String greeting() 来修复它,所以它就像
@GetMapping("/greeting")
public String greeting(@RequestParam(name="name", required=false, defaultValue="World") String name, Greeting greeting, Model model) {
model.addAttribute("name", name);
return "greeting.html";
}
虽然我不知道为什么会这样,希望有人能解释一下
你能试试吗:
@GetMapping("/greeting")
public String greeting(@RequestParam(name="name", required=false, defaultValue="World") String name, Greeting greeting, Model model) {
model.addAttribute("name", name);
model.addAttribute("greeting", new Greeting());
return "greeting.html";
}