简体   繁体   English

如何制作 Thymeleaf url 表达式并将其绑定到 @RequestParam 注释

[英]How to make a Thymeleaf url expression and bind it to a @RequestParam annotation

My form is based on Thymeleaf, I am not using tables because it has many fields.我的表单基于 Thymeleaf,我没有使用表格,因为它有很多字段。

In the form I have 3 buttons, one is a submit and the others are links.在表单中我有 3 个按钮,一个是提交,其他是链接。 With a link button I want to make a query using a parameter that sends a string to the controller.使用链接按钮,我想使用将字符串发送到控制器的参数进行查询。

I want to get the string that was passed in the URL to the controller and receive the data with an @RequestParam annotation.我想获取在 URL 中传递给控制器​​的字符串并接收带有 @RequestParam 注释的数据。

I have no error messages, what happens is that the string reaches the controller empty.我没有错误消息,结果是字符串到达​​控制器为空。

This is my link button:这是我的链接按钮:

 <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>

This is my text field where the user places the query:这是用户放置查询的文本字段:

 <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>

And all this goes inside my form:所有这些都包含在我的表单中:

 <form th:action="@{/config/item/items/save}" method="get" th:model="${item}">

I have used these two but with no results:我已经使用了这两个但没有结果:

 th:href="@{'/config/item/items/select'+${item.productId}}"

 th:href="@{|/config/item/items/select${item.productId}|}"

I have reviewed the documentation provided by Thymeleaf at: https://www.thymeleaf.org/doc/articles/standardurlsyntax.html , in chapter number nine.我已经查看了 Thymeleaf 提供的文档: https ://www.thymeleaf.org/doc/articles/standardurlsyntax.html,第 9 章。 And I have also seen tutorials but it doesn't work.我也看过教程,但它不起作用。

If you want to use the @RequestParam annotation you need to add a parameter in your link.如果要使用 @RequestParam 注释,则需要在链接中添加一个参数。 Your code should be something like:你的代码应该是这样的:

  th:href="@{select(productId=${item.productId})"

(According you added the object item to your template) (根据您将对象项添加到模板中)

In your controller "select" you have to use the annotation @RequestParam to retrieve the info:在您的控制器“选择”中,您必须使用注释 @RequestParam 来检索信息:

 @GetMapping (path = "/select")
 public String list(Model model,
        @RequestParam(name = "productId") int productId) { 
 // rest of the code
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM