英文:
How to make a Thymeleaf url expression and bind it to a @RequestParam annotation
问题
我的表单基于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}">
<!-- 表单内容 -->
</form>
我已经尝试过下面这两种方式,但都没有结果:
th:href="@{'/config/item/items/select' + ${item.productId}}"
th:href="@{|/config/item/items/select${item.productId}|}"
我已经阅读了Thymeleaf提供的文档:https://www.thymeleaf.org/doc/articles/standardurlsyntax.html,第九章节。我也看过一些教程,但都没有奏效。
英文:
My form is based on Thymeleaf, I am not using tables because it has many fields.
In the form I have 3 buttons, one is a submit and the others are links. 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.
I have no error messages, what happens is that the string reaches the controller empty.
This is my link button:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<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>
<!-- end snippet -->
This is my text field where the user places the query:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<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>
<!-- end snippet -->
And all this goes inside my form:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<form th:action="@{/config/item/items/save}" method="get"
th:model="${item}">
<!-- end snippet -->
I have used these two but with no results:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
th:href="@{'/config/item/items/select'+${item.productId}}"
<!-- end snippet -->
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
th:href="@{|/config/item/items/select${item.productId}|}"
<!-- end snippet -->
I have reviewed the documentation provided by Thymeleaf at: https://www.thymeleaf.org/doc/articles/standardurlsyntax.html, in chapter number nine.
And I have also seen tutorials but it doesn't work.
答案1
得分: 1
以下是翻译好的部分:
如果您想要使用 @RequestParam 注解,您需要在链接中添加一个参数。您的代码应该类似于:
th:href="@{select(productId=${item.productId})}"
(根据您已将对象 item 添加到您的模板中)
在您的控制器 "select" 中,您需要使用注解 @RequestParam 来检索信息:
@GetMapping (path = "/select")
public String list(Model model,
@RequestParam(name = "productId") int productId) {
// 其余代码
}
英文:
If you want to use the @RequestParam annotation you need to add a parameter in your link.
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:
@GetMapping (path = "/select")
public String list(Model model,
@RequestParam(name = "productId") int productId) {
// rest of the code
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论