如何解决胸腺模板解析误差

发布于 2025-01-26 22:15:09 字数 1761 浏览 2 评论 0原文

我正在尝试将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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光匆匆的小流年 2025-02-02 22:15:09

我尝试了您的程序,我认为您在浏览器中输入了错误的URL。因为您的代码没有错。

根据您的控制器:

@RequestMapping(value = { "/display-form", "/index.html" }, method = RequestMethod.GET)

,请确保您的浏览器的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:

@RequestMapping(value = { "/display-form", "/index.html" }, method = RequestMethod.GET)

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 your index.html without passing through your Controller. This is why you got a IllegalStateException with BindingResult issue.

enter image description here


But if you entered a correct url path, then you will see expected page:

enter image description here

enter image description here

若水般的淡然安静女子 2025-02-02 22:15:09

仅在错误消息中,我会假设您要填充的目标对象缺失或您当前提供的方式不起作用。
您可以调整控制器方法以看起来像

@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位于资源/模板中

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

@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";
}

Assuming your index.html is located in resources/templates

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文