【问题标题】:create hidden input with thymeleaf data使用百里香数据创建隐藏输入
【发布时间】:2020-08-30 19:54:37
【问题描述】:

所以基本上我的 html 页面中有这个表,它几乎可以正常工作

<div th:each="good : ${goodList}">
<form action="#" th:action="@{/zamow}"
th:object="${enterGoodAction}" method="post">
    <tr>
   <input type="hidden" path="id" value="${good.id}"/> //this input
   <th><span th:text="${good.name}"/></th>
   <th><span th:text="${good.amount}"/></th>
   <th><span th:text="${good.price}" /></th>
   <th><span th:text="${good.tax}" /></th>
   <th><input type="number" min="1" th:field="*{amount}"/></th>
   <th><input type="submit" value="Zamów" /></th>
   </tr>

</form>

现在我要做的是创建这个隐藏的输入,它将“good.id”数据传递给我的控制器,但无论我对他做什么,他总是为空。我该如何解决?我认为控制器运行良好,所以问题只出在我的输入上。实际输入可能看起来很傻,但这是我的第 10 次尝试或其他什么,我很绝望;/

【问题讨论】:

    标签: java html thymeleaf


    【解决方案1】:

    在表单中访问对象的内部属性需要这样做

    <input type="hidden" th:field="*{good.id}" />
    

    我假设你有一个具有这种结构的对象

    enterGoodAction.good.id
    

    如果不是这样,找到从 enterGoodAction 对象到 ID 的正确路径

    【讨论】:

      【解决方案2】:

      您刚刚使用了value,这是一个无聊的属性并且按原样传递。请改用th:value(或data-th-value)。

      【讨论】:

      • 也尝试使用 th:value 仍然为空 ;/
      • @Vesspe 那么你没有清楚地描述结果。您说“null”,但属性不能为 null(至少在 HTML 中)。它可以是不存在的,它可以是空的,它可以是无效的。显示呈现的 HTML。
      • @Vesspe 如果你说它没有提交,那是因为input 没有name...
      • null 我的意思是我的控制器看到的应该是来自这个输入的数据。需要阅读有关渲染 html 的信息,因为我从未听说过。
      • @Vesspe 渲染 HTML 是 Thymeleaf 的重点。您需要了解它是如何工作的。
      【解决方案3】:

      好吧,我遇到了一个奇怪的问题,我无法在 thymeleaf 中设置 &lt;input type="hidden"&gt; 字段的“值”属性。对我有用的可能会被认为是 jQuery hack 的方法是

      &lt;input id = "good-id" type="hidden"/&gt;

      百里香叶

      <script th:inline="javascript">   
             $(document).ready(function() {
             var goodIdValue = /*[[${good.id}]]*/'null';
             $("#good-id").val(goodIdValue);
             });
      </script>
      

      这使用jQuery设置隐藏字段的类型value属性

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-10-31
        • 2018-11-08
        • 1970-01-01
        • 2019-06-13
        • 2014-08-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多