java - th :disabled objects in Thymeleaf

标签 java spring spring-mvc spring-boot 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>

在我的 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;
        }

我也试过只读,但它没有作为选项出现在选择对象上

enter image description here

最佳答案

如果您希望发送值但不希望用户能够编辑它,您应该选择使用readonly

disabledreadonly 的使用有细微差别

比较

  • 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 while disabled one are not.

关于java - th :disabled objects in Thymeleaf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52464940/

相关文章:

java - 为什么java中静态需要连接<T>?

Java 网络阻塞方法和 while 循环

java - Spring安全@PreAuthorize NullPointerException。为什么?

java - etat HTTP 404 -/mvc/traitementTwo.jsp - spring mvc

validation - 如何从表单中的集合中选择实体? Spring MVC 和 Thymeleaf

java - Spring MVC @RequestParam 值未按预期从 URI 中提取

java - 如何使用数字格式类格式化货币

java - 初始化抽象类的成员,子类没有写访问权限

java - 你好如何在新响应中设置java中的内容

javascript - 如何将Spring mvc Controller 变量传递给javascript