我想在data
中显示table row
。然而,它只是将var name
显示为输出,例如:
Name | age
User1| {age}
我正在使用Spring-MVC
和Spring Boot
。我已经创建了其他映射类,并且它们的数据在HTML <p> tags
中正确显示。
我的控制器:
@Controller
public class MyController {
@RequestMapping("/age")
public String age(@RequestParam(value="age", required=false, defaultValue="5") String age, Model model) {
model.addAttribute("age", age);
return "age";
}
}
age.html :(使用ThymeLeaf)
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Age Table</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<table border="1">
<tr>
<td>Name</td>
<td>Age</td>
</tr>
<tr>
<td>User1</td>
<td>${age}</td>
</tr>
</table>
</body>
</html>
答案 0 :(得分:1)
试试这个:
<table>
<tr>
<th>Name</th>
<th>Age</th>
</tr>
<tr>
<td>User 1</td>
<td th:text="${age}"> Age </td>
</tr>
</table>
如果您有这样的年龄列表:
List<Integer> listOfAge = new ArrayList<>(Arrays.asList(23,45,45,23,54,23,43));
model.addAttribute("listOfAge", listOfAge);
然后试试这个:
<table>
<tr>
<th>Sr. No</th>
<th>Name</th>
<th>Age</th>
</tr>
<tr th:each="age,iterationstatus :${listOfAge}">
<td th:text="${iterationstatus.count}">1</td>
<td>User 1</td>
<td th:text="${age}"> Age </td>
</tr>
</table>