thymeleaf如何遍历Controller层中的List<>
时间: 2023-06-06 19:08:28 浏览: 62
Thymeleaf提供了一些内置的迭代器用于遍历集合,其中包括th:each,可以将一个集合中的元素逐个进行渲染。在Controller中,可以通过将List传递到模板中,在模板中使用th:each来遍历List。例如,假设Controller中有一个名为userList的List,可以在模板中使用以下代码来遍历这个List中的元素:
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
</tr>
</thead>
<tbody>
<tr th:each="user : ${userList}">
<td th:text="${user.id}"></td>
<td th:text="${user.name}"></td>
<td th:text="${user.age}"></td>
</tr>
</tbody>
</table>
在上面的代码中,th:each="user : ${userList}"表示遍历userList集合,并将每个元素赋值给名为user的变量。然后就可以使用th:text将该用户的属性渲染到表格中。
相关问题
ssm thymeleaf获取controller 重定向返回list集合
可以使用redirect前缀将请求重定向到Controller中的另一个方法,然后在这个方法中获取list集合并返回给Thymeleaf模板。
例如,假设我们有一个Controller类,其中有两个方法:一个用于返回表单页面,另一个用于处理表单提交并重定向到另一个Controller方法以获取列表并返回给Thymeleaf模板。
```java
@Controller
public class MyController {
@GetMapping("/form")
public String showForm(Model model) {
model.addAttribute("myObject", new MyObject());
return "myForm";
}
@PostMapping("/submit")
public String handleSubmit(@ModelAttribute("myObject") MyObject myObject) {
// 处理表单提交并重定向到list方法
return "redirect:/list";
}
@GetMapping("/list")
public String showList(Model model) {
List<MyObject> myObjects = // 获取对象列表
model.addAttribute("myObjects", myObjects);
return "myList";
}
}
```
在上面的示例中,`showForm`方法返回表单页面,`handleSubmit`方法处理表单提交并重定向到`showList`方法以获取对象列表。在`showList`方法中,我们可以获取对象列表并将其添加到模型中,然后返回列表页面。
在Thymeleaf模板中,我们可以使用`th:if`指令检查模型中是否存在对象列表,如果存在,则可以使用`th:each`指令遍历列表并显示每个对象的属性。
```html
<!-- myForm.html -->
<form th:action="@{/submit}" th:object="${myObject}" method="post">
<!-- 表单字段 -->
</form>
<!-- myList.html -->
<div th:if="${myObjects}">
<table>
<thead>
<tr>
<th>属性1</th>
<th>属性2</th>
<!-- ... -->
</tr>
</thead>
<tbody>
<tr th:each="myObject : ${myObjects}">
<td th:text="${myObject.property1}"></td>
<td th:text="${myObject.property2}"></td>
<!-- ... -->
</tr>
</tbody>
</table>
</div>
```
SpringBoot+Thymeleaf中controller修改方法怎么写
在SpringBoot+Thymeleaf中,Controller中的修改方法通常需要以下步骤:
1. 在Controller中定义一个处理修改请求的方法,例如:
```java
@RequestMapping(value = "/edit/{id}", method = RequestMethod.POST)
public String edit(@PathVariable("id") Long id, Model model, @ModelAttribute("form") MyForm form) {
// 根据id查询要修改的数据
MyData myData = myDataService.findById(id);
if (myData == null) {
throw new NotFoundException();
}
// 将form中的数据赋值给myData
myData.setName(form.getName());
myData.setValue(form.getValue());
// 更新数据
myDataService.save(myData);
// 重定向到列表页面
return "redirect:/list";
}
```
2. 在Thymeleaf模板中,使用form表单提交修改请求,例如:
```html
<form th:action="@{/edit/{id}(id=${myData.id})}" method="post" th:object="${form}">
<div class="form-group">
<label for="name">Name</label>
<<input type="text" class="form-control" id="name" name="name" th:value="*{name}" />
</div>
<div class="form-group">
<label for="value">Value</label>
<input type="text" class="form-control" id="value" name="value" th:value="*{value}" />
</div>
<button type="submit" class="btn btn-primary">Save</button>
</form>
```
3. 在Controller中,需要将修改前的数据显示在修改页面上,例如:
```java
@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
public String edit(@PathVariable("id") Long id, Model model) {
// 根据id查询要修改的数据
MyData myData = myDataService.findById(id);
if (myData == null) {
throw new NotFoundException();
}
// 将数据传递给form对象
MyForm form = new MyForm();
form.setName(myData.getName());
form.setValue(myData.getValue());
// 将form对象传递给页面
model.addAttribute("form", form);
return "edit";
}
```
这样,在访问修改页面时,会将修改前的数据显示在表单中,用户可以直接修改并提交。