如何解决如何制作Thymeleaf网址表达式并将其绑定到@RequestParam注释
我的表单基于Thymeleaf,我不使用表,因为它具有许多字段。
在表单中,我有3个按钮,一个是提交,另一个是链接。我想使用链接按钮使用向字符串发送字符串到控制器的参数进行查询。
我想获取URL中传递给控制器的字符串,并使用@RequestParam批注接收数据。
我没有任何错误消息,发生的是字符串到达控制器为空。
这是我的链接按钮:
<a class="btn btn-success text-white btn-sm mr-3 " style="border-radius: 16px;" th:href="@{/config/item/items/select(productId=${item.productId})}">
Search
</a>
这是我在其中输入用户查询的文本字段:
<div class="col-2 form-group"
th:classappend="${#fields.hasErrors('item.productId')}? 'has-error':''">
<label for="productId"><strong>Item id </strong></label> <input
type="text" class="form-control" id="productId" name="productId"
th:field="${item.productId}">
</div>
所有这些都在我的表单中:
<form th:action="@{/config/item/items/save}" method="get"
th:model="${item}">
我使用了这两个,但没有结果:
th:href="@{'/config/item/items/select'+${item.productId}}"
th:href="@{|/config/item/items/select${item.productId}|}"
我在第九章的https://www.thymeleaf.org/doc/articles/standardurlsyntax.html中查看了Thymeleaf提供的文档。 而且我也看过教程,但这没用。
解决方法
如果要使用@RequestParam批注,则需要在链接中添加参数。 您的代码应类似于:
th:href="@{select(productId=${item.productId})"
(因此,您已将对象项添加到模板中)
在控制器“选择”中,您必须使用注释@RequestParam来检索信息:
@GetMapping (path = "/select")
public String list(Model model,@RequestParam(name = "productId") int productId) {
// rest of the code
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。