thymeleaf - 如何为 Thymeleaf 输入字段添加默认值?
问题描述
这是我的百里香表单输入字段。我想为此设置一个默认值。假设我想将“个人”设置为默认值。怎么做?
<div class="col-sm-9">
<label th:if="${#fields.hasErrors('priceSearchDTO.partyType')}" th:errors="*{priceSearchDTO.partyType}"
class="validation-message"></label>
<input type="text" th:field="*{priceSearchDTO.partyType}" placeholder="partyType"
class="form-control"/>
</div>
另外我试过这个,但它不能像普通的 html 字段那样工作。
<div class="col-sm-9">
<label th:if="${#fields.hasErrors('priceSearchDTO.partyType')}" th:errors="*{priceSearchDTO.partyType}"
class="validation-message"></label>
<input type="text" th:field="*{priceSearchDTO.partyType}" placeholder="partyType" value="individual"
class="form-control"/>
</div>
解决方案
有一个 th:placeholder 属性。您可以使用此属性动态设置占位符属性的值,例如:
<input ... th:placeholder="${#object.individualPlaceholder}" ...>
或者
<input ... th:placeholder="|Static and ${#object.dynamic}|" ...>
如果您的占位符是静态值,您仍然可以使用占位符属性:
<input ... placeholder="Insert something" ...>
也可以为 th:placeholder 使用静态值。
推荐阅读
- git - Git - 为什么我的源文件被忽略?
- c# - InsertMany() 给出 FormatException,我尝试处理它会导致重复键错误
- sql - 在 where 条件下声明 varchar
- html - 制作2个面板并在一页html中加载2个网站,并放大缩小而不影响其他面板
- elasticsearch - 如果将确切的单词用作文本,elasticsearch 不会提出任何建议吗?
- php - 如果数据丢失,php 不会回显错误消息 - 而是显示 php 致命错误
- firebase - 大尺寸数组的权限缺失或不足
- c - 如何将2添加到C中的4位数字中的每个数字
- javascript - 更新 Redux 存储时反应组件不重新渲染
- ubuntu - 无法在 ubuntu 18.04 上安装 php-mbstring