【问题标题】:How to set default value in thymeleaf th:field如何在 thymeleaf th:field 中设置默认值
【发布时间】:2018-06-27 05:44:07
【问题描述】:

我有一个表单,我想在下面的字段中设置默认值,但它不起作用。

    <span>ID User:</span>

                <input type="text"   th:value="${session.name}"  th:field="*{userid}" th:errorclass="field-error"  />

            </div>  
            <div>           
                <span class="name-in">ID Room:</span>
                <input type="text"  th:value="${id}"   th:field="*{room}" th:errorclass="field-error" />
            </div>      

我阅读了一些有关此问题的主题,并尝试按以下方式进行更改

th:attr="value = ${session.name}" 

但它仍然无法正常工作。字段 ID 用户为空。我不知道如何解决这个问题。

【问题讨论】:

  • 为了清楚起见,您想填充您的“userId”字段,然后在提交按钮后将该对象进一步发送到下一个控制器。?

标签: spring thymeleaf


【解决方案1】:

虽然您的问题包含的信息较少,但我认为您想为所有字段设置默认值。如果你喜欢这样做改变

`<input type="text"   th:value="${session.name}"  th:field="*{userid}" th:errorclass="field-error"  />`

<input type="text"   name="userid" value="as your wish" th:errorclass="field-error"  />

【讨论】:

    【解决方案2】:

    您应该在控制器中设置 *{userid} 的值,而不是更改 html。这样你就可以保持你的 html 不变:

    // Controller
    modelObject.setUserId(session.name);
    
    // HTML
    <input type="text" th:field="*{userid}" th:errorclass="field-error"  />
    

    【讨论】:

      猜你喜欢
      • 2019-03-22
      • 2019-07-11
      • 2020-09-17
      • 1970-01-01
      • 2020-09-27
      • 2013-12-20
      • 2010-11-14
      • 2021-09-22
      • 2020-01-18
      相关资源
      最近更新 更多