如何解决胸腺模板解析误差
我正在尝试将HTML呈现到屏幕上,但我会从百里香中获得模板解析器错误。 这是我的代码和错误:
index.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:@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;
}
}
mymodel:
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中删除输入时,它可以很好地工作,因此问题在于将百里叶领域与MyModel变量匹配,但我无法解决。
I'm trying to present a html to screen but I'm getting template parser error from thymeleaf.
Here is my code and error:
index.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:
@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;
}
}
MyModel:
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;
}
}
Error:
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
I tried something but not helped. When I delete the inputs from HTML it works finely so problem is about matching thymeleaf fields with MyModel variables but I couldn't solve.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我尝试了您的程序,我认为您在浏览器中输入了错误的URL。因为您的代码没有错。
根据您的控制器:
,请确保您的浏览器的URL是:
&lt; host&gt;:&lt; port&gt;/&lt;/&lt; context-path;/index.htex.html
或&lt; host&gt;:&lt; port&gt;/&lt; context-path;/display-form
而不是
&lt; host&gt;:&lt; - path&gt;/
。问题是,
&lt; host&gt;:&lt; port&gt;/&lt;/&lt context-path&gt;/
是默认的欢迎页面,而spring直接呈现您的index.html
而无需通过通过您的控制器。这就是为什么您获得了带有绑定问题的IllegalStateException的原因。但是,如果您输入了正确的URL路径,那么您将看到预期的页面:
”
I have tried your program, and I consider that you entered a wrong url in your browser. Because there is nothing wrong in your code.
According to your controller:
Please make sure your browser's url is:
<host>:<port>/<context-path>/index.html
or<host>:<port>/<context-path>/display-form
instead of
<host>:<port>/<context-path>/
.The problem is,
<host>:<port>/<context-path>/
is the default welcome page and Spring directly renders yourindex.html
without passing through your Controller. This is why you got a IllegalStateException with BindingResult issue.But if you entered a correct url path, then you will see expected page:
仅在错误消息中,我会假设您要填充的目标对象缺失或您当前提供的方式不起作用。
您可以调整控制器方法以看起来像
假设您的index.html位于资源/模板中
Just from the error message I would assume the target object you want to fill is missing or the way you currently provide it does not work.
You can adjust your controller method to look like
Assuming your index.html is located in resources/templates