java 使用 thymeleaf 数据创建隐藏输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41840221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
create hidden input with thymeleaf data
提问by Vesspe
So basically I have this table in my html page and its almost work as it should
所以基本上我的 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>
Now what I want to do is create this hidden input which will pass me "good.id" data to my controller but whatever i do with him he is always null. How can i fix it? I think controller works well so the problem is with my input only. Actual imput might looks silly but its my 10th try or something and I was desperate ;/
现在我想做的是创建这个隐藏的输入,它将我的“good.id”数据传递给我的控制器,但无论我对他做什么,他总是为空。我该如何解决?我认为控制器运行良好,所以问题仅在于我的输入。实际输入可能看起来很傻,但这是我的第 10 次尝试,我很绝望;/
回答by cralfaro
The way you need to do to access an internal property of the object in the form is this
访问表单中对象的内部属性所需的方法是这样的
<input type="hidden" th:field="*{good.id}" />
I am assuming that you have an object with this structure
我假设你有一个具有这种结构的对象
enterGoodAction.good.id
If is not the case find the right path from enterGoodAction object to the ID
如果不是这种情况,请找到从 enterGoodAction 对象到 ID 的正确路径
回答by chrylis -cautiouslyoptimistic-
You just used value
, which is a boring attribute and gets passed as-is. Use th:value
(or data-th-value
) instead.
您刚刚使用了value
,这是一个无聊的属性并按原样传递。改用th:value
(或data-th-value
)。