java - th:Thymeleaf 中禁用的对象
问题描述
我有一个基本的 SpringBoot 2.0.5.RELEASE 应用程序。使用 Spring Initializer、JPA、嵌入式 Tomcat、Thymeleaf 模板引擎,并将其打包为可执行的 JAR 文件。
我有一个只读的选择对象,
<select id="selectInvoiceId" th:field="*{invoice}" th:disabled="${resto.id != null}" >
<option value="0">PLEASE SELECT AN INVOICE</option>
<option th:each="invoice : ${invoices}"
th:value="${invoice.id}"
th:text="${invoice.symbol}">
</option>
</select>
在我的控制器中
@RequestMapping(value = { "/save" }, method = RequestMethod.POST)
public String saveWallet(@Valid @ModelAttribute("wallet") WalletPayload walletPayload,
BindingResult bindingResult) {
..
}
在 WalletPayload 对象中我有:
@NotNull
private Invoice invoice;
然后我在验证中总是出错,因为发票为空,我想知道只读对象是否有解决方法
我已经尝试过了,但我仍然有错误:
@RequestMapping(value = { "/save" }, method = RequestMethod.POST)
public String saveWallet(@Valid @ModelAttribute("wallet") WalletPayload walletPayload,
BindingResult bindingResult) {
LOG.debug("WalletPayload walletPayload [ " + walletPayload + " ]");
if (walletPayload.getId() != null) {
Invoice fakeInvoine = new Invoice("fake-inv");
fakeInvoice.setId((long)-1);
walletPayload.setInvoice(fakeInvoice);
}
if (bindingResult.hasErrors()) {
return serverContextPath + WALLET_LIST_VIEW_NAME;
}
我也尝试使用只读,但它没有作为选择对象上的选项出现
解决方案
readonly
如果您希望发送值而不希望用户能够编辑它,则应该选择使用。
disabled
使用和之间存在细微差别readonly
比较
readonly
项目不可编辑,但一旦提交就会发送。disabled
项目不可编辑,一旦提交就不会发送。readonly
项目可以聚焦,而disabled
一个则不能。
推荐阅读
- performance - 只读操作的回写和直写内存之间的巨大性能差异
- reactjs - React 组件无法访问 Redux 存储
- debugging - 视觉工作室代码调试百里香
- java - 如何从控制器中的 th:value (thymeleaf) 中检索数据?
- r - 在 R 中合并两个相同的两个相同的小标题以折叠它们的缺失值
- scala - 无法反序列化元组
- c# - DisplayNameFor 有什么作用?
- r - 使用重置为 1 的计数器设置数据帧并为每个子集创建数据帧
- sql-server - 如何将 VBA 字符串传递给 SQL Server ADODB 查询?
- python - 默认情况下不能取消选中单选按钮