Spring Thymeleaf 文本区域

新手上路,请多包涵

我用一个输入文本和一个 textarea 创建了表单。输入文本工作正常,但 textarea 甚至不显示:

 <div id="news" th:fragment="admin_panel">
    <form method="POST" th:action="@{/addNews}" th:object="${news}" id="myform">
        Tytuł:
        <input type="text" th:field="*{title}"/>
        <input type="submit" value="Wstaw"/>
    </form>
    <textarea name="news_content" rows="20" cols="80" th:field="${news.content}" form="myform">
        ...
    </textarea>
</div>

When I delete the th:field the textarea is displayed and when I use th:value instead of th:field it’s displayed too but doesn’t save the将文本写入 news.content(news.title 保存正常)。

我不知道……我读了 thymeleaf 参考资料但找不到答案,所以请帮助好人!

原文由 Bambelal 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 424
2 个回答

您必须使用选定的对象表达式 *{content} 并将 textarea 标签放在 form 标签内!

最后,所有关于生成的 name 属性的结果形式。该名称需要对应于所选根对象中的 propertyAccessor th:object 。表单由 spring 处理(没有任何 thymeleaf 拦截)。

关于 spring 集成的文档非常好: http ://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html

他们这样说:

th:field 属性的值必须是选择表达式 (*{…}),这是有道理的,因为它们将在表单支持 bean 而不是上下文变量(或 Spring MVC 术语中的模型属性)上进行评估).


编辑:感谢项目链接,修复很简单:

  • Thymeleaf 3.0.0.BETA03 在 textarea 处理器中有一个错误,移动到 3.0.0.RELEASE 修复了这个问题
  • 此外,我已将文本区域移动到表单元素内。

原文由 Martin Frey 发布,翻译遵循 CC BY-SA 3.0 许可协议

当我通过它保存一些数据时,我的 textarea 输入工作正常(当我执行保存到数据库时处于干净状态)但是在编辑表单的情况下(我的 textarea 输入应该显示模型属性书中的预填充描述.description) 是空白的,原因是 th:value 属性,我将其更改为 th:field 属性,它开始按预期工作。

 <textarea class="form-control" id="description" rows="5"
                              name="description"
                              placeholder="Description" th:field="${book.description}"
                              required="required"></textarea>

原文由 Rajan Chauhan 发布,翻译遵循 CC BY-SA 4.0 许可协议

推荐问题