【问题标题】:Thymeleaf Input Form BindingThymeleaf 输入表单绑定
【发布时间】:2019-10-21 22:53:59
【问题描述】:

我有一个绑定到我的班级的表格,一切正常。

<form th:action="@{/createPost}" th:object="${newPost}" method="post" class="form-signin">

        <input type="text" th:field="*{topic}" class="new-post-topic-input" placeholder="post topic"
               autofocus="true"/>
</form>

我想将我的输入 css 更改为不同的样式,但 type='text' 会覆盖所有内容。但是,如果我将输入类型更改为其他任何内容,它就不会绑定值。 我可以让 thymeleaf 查看其他输入类型来绑定它们吗?

更新

我有包含 css 样式的引导 css 文件

但我想在不从页面中删除引导 css 的情况下对此输入有自己的风格。但是,如果我将 class='anyClass' 添加到此输入中,则来自 type='text' 的引导程序中的 css 会覆盖所有内容。如果我将类型从文本更改为其他任何内容,百里香不再映射值。

更新2 我的问题与 CSS 覆盖无关。

我的问题是关于 thymeleaf 数据对象绑定。我有 th:object="${newPost}" 在表单标签中。 Thymeleaf 看到它,当我提交表单时,它会将每个 , 绑定到该对象的字段。

但如果我从 type='text' 更改为 type='ANYTHING ELSE'> thymeleaf 不会映射任何内容。如何让 thymeleaf 将其他标签(如 etc.)绑定到我的对象?

【问题讨论】:

标签: java html thymeleaf


【解决方案1】:

The documentation 明确表示可以绑定其他类型:

注意th:field还理解HTML5引入的&lt;input&gt;元素的新类型,如&lt;input type="datetime" ... /&gt;&lt;input type="color" ... /&gt;等,有效地为Spring MVC添加了完整的HTML5支持。

文档还提供了一个示例:

<div>
  <label th:for="${#ids.next('covered')}" th:text="#{seedstarter.covered}">Covered</label>
  <input type="checkbox" th:field="*{covered}" />
</div>

当然,covered 字段必须是 boolean 类型。请确保您的 input 元素的类型与您的字段类型匹配。

【讨论】:

  • 所以基本上它了解基本类型,但不了解我的类型。
  • 我无法回答这个问题,因为我不知道“你的”类型是什么。
猜你喜欢
  • 2015-02-01
  • 2016-02-28
  • 2015-08-06
  • 2018-04-01
  • 2021-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多