【问题标题】:thymeleaf th:field returns erros when i use the th:field当我使用 th:field 时,thymeleaf th:field 返回错误
【发布时间】:2019-02-08 01:07:53
【问题描述】:

当我使用这样的输入时,我没有问题:

<input type="text" name="title" />

但是如果我把 th:name 放在我得到错误 500 的地方:

<input type="text" th:field="${title}"/> 

Começo a tomar erro 500,conforme abaixo

这里是 git 存储库:https://github.com/getJv/springStudy

这是错误:

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Thu Feb 07 23:03:56 BRST 2019
There was an unexpected error (type=Internal Server Error, status=500).
An error happened during template parsing (template: "class path resource [templates/books/form.html]")
org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/books/form.html]")

【问题讨论】:

    标签: spring spring-boot spring-mvc thymeleaf


    【解决方案1】:

    请参阅此处的Thymeleaf Documentation

    th:field 用于在与带有上下文变量的 th:object 配对时自动填充值。

    如果你想动态渲染标签的名字,你应该使用 th:name 来代替


    下面的示例将显示“小熊维尼”作为值

    BookController.java

    Book book = new Book();
    book.setTitle("Winnie the Pooh");
    
    ModelAndView mv = new ModelAndView("book/form");
    mv.addObject("book", book);
    

    百里香叶:

    <form th:object="${book}">
        <input type="text" th:field="*{title}">
    </form>
    

    【讨论】:

    • 哦@Chris 它真的有效。但现在我有一个疑问......所以我是否希望在 m 表单中使用 th:object/field 我需要创建一个新的主表单类的空实例?像这样的东西? @RequestMapping("/form") public ModelAndView form() { ModelAndView mv = new ModelAndView("books/form"); Book book = new Book(); mv.addObject("book", book); mv.addObject("priceTypes", PriceType.values()); return mv; }
    • 比你。这救了我的命。 :D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-07
    • 1970-01-01
    • 2013-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多