如何创建一个Thymeleaf URL表达式并将其绑定到@RequestParam注解。

huangapple go评论78阅读模式
英文:

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

&lt;a class=&quot;btn btn-success text-white btn-sm mr-3 &quot; style=&quot;border-radius: 16px;&quot; th:href=&quot;@{/config/item/items/select(productId=${item.productId})}&quot;&gt;
		Search 	
	&lt;/a&gt;

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

	&lt;div class=&quot;col-2 form-group&quot;
		th:classappend=&quot;${#fields.hasErrors(&#39;item.productId&#39;)}? &#39;has-error&#39;:&#39;&#39;&quot;&gt;
		&lt;label for=&quot;productId&quot;&gt;&lt;strong&gt;Item id &lt;/strong&gt;&lt;/label&gt; &lt;input
			type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;productId&quot; name=&quot;productId&quot;
			th:field=&quot;${item.productId}&quot;&gt;
	&lt;/div&gt;

<!-- end snippet -->

And all this goes inside my form:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

		&lt;form th:action=&quot;@{/config/item/items/save}&quot; method=&quot;get&quot;
			th:model=&quot;${item}&quot;&gt;

<!-- 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=&quot;@{&#39;/config/item/items/select&#39;+${item.productId}}&quot;

<!-- end snippet -->

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

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

<!-- 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=&quot;@{select(productId=${item.productId})&quot;

(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 = &quot;/select&quot;)
 public String list(Model model,
		@RequestParam(name = &quot;productId&quot;) int productId) { 
 // rest of the code
 }

huangapple
  • 本文由 发表于 2020年8月30日 22:53:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63658708.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定