我有一个基本的 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>
在我的 Controller 中
@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
items are not editable, but will be sent when once submited.disabled
items are not editable and are not sent once submited.readonly
items are focus-able whiledisabled
one are not.
关于java - th :disabled objects in Thymeleaf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52464940/