Spring Request Parameters with Thymeleaf – 使用Thymeleaf的Spring请求参数

最后修改: 2019年 7月 27日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.绪论

In our article Introduction to Using Thymeleaf in Spring, we saw how to bind user input to objects.

在我们的文章Introduction to Using Thymeleaf in Spring中,我们看到了如何将用户输入绑定到对象。

We used th:object and th:field in the Thymeleaf template and @ModelAttribute in the controller to bind data to a Java object. In this article, we’ll look at how to use the Spring annotation @RequestParam in combination with Thymeleaf.

我们在Thymeleaf模板中使用th:objectth:field,在控制器中使用@ModelAttribute,将数据绑定到Java对象。在这篇文章中,我们将探讨如何将Spring注解@RequestParam与Thymeleaf结合使用。

2. Parameters in Forms

2.表格中的参数

Let’s first create a simple controller that accepts four optional request parameters:

让我们首先创建一个简单的控制器,接受四个可选的请求参数。

@Controller
public class MainController {
    @RequestMapping("/")
    public String index(
        @RequestParam(value = "participant", required = false) String participant,
        @RequestParam(value = "country", required = false) String country,
        @RequestParam(value = "action", required = false) String action,
        @RequestParam(value = "id", required = false) Integer id,
        Model model
    ) {
        model.addAttribute("id", id);
        List<Integer> userIds = asList(1,2,3,4);
        model.addAttribute("userIds", userIds);
        return "index";
    }
}

The name of our Thymeleaf template is index.html. In the following three sections, we’ll use different HTML form elements for the user to pass data to the controller.

我们的Thymeleaf模板的名字是index.html在以下三个部分,我们将使用不同的HTML表单元素,让用户将数据传递给控制器。

2.1. Input Element

2.1.输入元素

First, let’s create a simple form with a text input field and a button to submit the form:

首先,让我们创建一个简单的表单,有一个文本输入字段和一个按钮来提交表单。

<form th:action="@{/}">
<input type="text" th:name="participant"/> 
<input type="submit"/> 
</form>

The attribute th:name=”participant” binds the value of the input field to the parameter participant of the controller. For this to work, we need to annotate the parameter with @RequestParam(value = “participant”).

属性th:name=”participant”将输入字段的值与控制器的参数participant绑定。为了使其发挥作用,我们需要用@RequestParam(value = “participant”)来注释该参数。

2.2. Select Element

2.2.选择要素

Likewise for the HTML select element:

同样,对于HTML选择元素也是如此。

<form th:action="@{/}">
    <input type="text" th:name="participant"/>
    <select th:name="country">
        <option value="de">Germany</option>
        <option value="nl">Netherlands</option>
        <option value="pl">Poland</option>
        <option value="lv">Latvia</option>
    </select>
</form>

The value of the selected option is bound to the parameter country, annotated with @RequestParam(value = “country”).

所选选项的值被绑定到参数country,注释为@RequestParam(value = “country”)

2.3. Button Element

2.3 按钮元素

Another element where we can use th:name is the button element:

另一个我们可以使用th:name的元素是按钮元素。

<form th:action="@{/}">
    <button type="submit" th:name="action" th:value="in">check-in</button>
    <button type="submit" th:name="action" th:value="out">check-out</button>
</form>

Depending on whether the first or second button is pressed to submit the form, the value of the parameter action will be either check-in or check-out.

根据是按下第一个还是第二个按钮来提交表单,参数action的值将是check-incheck-out

3. Parameters in Hyperlinks

3.超链接中的参数

Another way to pass request parameters to a controller is via a hyperlink:

另一种向控制器传递请求参数的方式是通过超链接:

<a th:href="@{/index}">

And we can add parameters in parentheses:

而且我们可以在括号里添加参数。

<a th:href="@{/index(param1='value1',param2='value2')}">

Thymeleaf evaluates the above to:

Thymeleaf对上述内容的评价是:。

<a href="/index?param1=value1&param2=value2">

Using Thymeleaf expressions to generate hyperlinks is particularly useful if we want to assign parameter values based on variables. For example, let’s generate a hyperlink for each user ID:

如果我们想根据变量分配参数值,使用Thymeleaf表达式来生成超链接特别有用。例如,让我们为每个用户ID生成一个超链接。

<th:block th:each="userId: ${userIds}">
    <a th:href="@{/(id=${userId})}"> User [[${userId}]]</a> <br/>
</th:block>

We can pass a list of user IDs as a property to the template:

我们可以将用户ID的列表作为一个属性传递给模板。

List<Integer> userIds = asList(1,2,3);
model.addAttribute("userIds", userIds);

And the resulting HTML will be:

而产生的HTML将是。

<a th:href="/?id=1"> User 1</a> <br/>
<a th:href="/?id=2"> User 2</a> <br/>
<a th:href="/?id=3"> User 3</a> <br/>

The parameter id in the hyperlink is bound to the parameter id, annotated with @RequestParam(value = “id”).

超链接中的参数id被绑定到参数id,用@RequestParam(value = “id”)来注释。

4. Summary

4.摘要

In this short article, we saw how to use Spring request parameters in combination with Thymeleaf.

在这篇短文中,我们看到了如何将Spring请求参数与Thymeleaf结合使用。

First, we created a simple controller that accepts request parameters. Second, we looked at how to use Thymeleaf to generate an HTML page that can call our controller.

首先,我们创建了一个接受请求参数的简单控制器。其次,我们研究了如何使用Thymeleaf来生成一个可以调用控制器的HTML页面。

The full source code for all examples in this article can be found on GitHub.

本文中所有例子的完整源代码可以在GitHub上找到。