我正在嘗試向螢屏顯示 html,但我從 thymeleaf 收到模板決議器錯誤。這是我的代碼和錯誤:
索引.html:
<body>
<h2>My ToDo List</h2>
<form action="/send-form-data" method="post" th:object="${myForm}">
<table>
<tr>
<td>Title:</td>
<td><input type="text" th:field="*{title}"/></td>
</tr>
<tr>
<td>Decription:</td>
<td><input type="text" th:field="*{description}"/></td>
</tr>
</table>
控制器:
@Controller
public class TodolistController {
@RequestMapping(value = { "/display-form", "/index.html" }, method = RequestMethod.GET)
public ModelAndView displayForm() {
ModelAndView mv = new ModelAndView("index");
mv.addObject("myForm", new MyModel());
return mv;
}
}
我的模型:
public class MyModel {
private String title;
private String description;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
錯誤:
Caused by: org.attoparser.ParseException: Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' (template: "index" - line 15, col 28)
Caused by: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'formData' available as request attribute
我嘗試了一些東西但沒有幫助。當我從 HTML 中洗掉輸入時,它作業得很好,所以問題是將 thymeleaf 欄位與 MyModel 變數匹配,但我無法解決。
uj5u.com熱心網友回復:
我試過你的程式,我認為你在瀏覽器中輸入了錯誤的 url。因為您的代碼沒有任何問題。
根據您的控制器:
@RequestMapping(value = { "/display-form", "/index.html" }, method = RequestMethod.GET)
請確保您的瀏覽器的網址是:
<host>:<port>/<context-path>/index.html或者<host>:<port>/<context-path>/display-form
而不是 <host>:<port>/<context-path>/.
問題是,<host>:<port>/<context-path>/是默認的歡迎頁面,而 Spring 直接呈現您index.html而不通過您的控制器。這就是為什么您遇到帶有 BindingResult 問題的 IllegalStateException 的原因。

但是,如果您輸入了正確的 url 路徑,那么您將看到預期的頁面:


uj5u.com熱心網友回復:
僅從錯誤訊息中,我會假設您要填充的目標物件丟失或您當前提供的方式不起作用。你可以調整你的控制器方法看起來像
@Controller
public class TodolistController {
@RequestMapping(value = { "/display-form", "/index.html" }, method = RequestMethod.GET)
public String displayForm(Model model) {
model.addAttribute("myForm", new MyModel());
return "index";
}
假設您的 index.html 位于資源/模板中
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/471012.html
上一篇:我如何使用Dockerfile將Maven依賴項存盤在Docker映像中
下一篇:使用Mono流處理例外
