问题
I am trying to display data in a table row. However it is just showing the var name as output, e.g:
Name | age
User1| {age}
I am using Spring-MVC along with Spring Boot. I have created other mapping classes and their data is showing correctly in HTML <p> tags.
My Controller:
@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: (uses 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>
回答1:
Try this :
<table>
<tr>
<th>Name</th>
<th>Age</th>
</tr>
<tr>
<td>User 1</td>
<td th:text="${age}"> Age </td>
</tr>
</table>
If you have a list of age like this:
List<Integer> listOfAge = new ArrayList<>(Arrays.asList(23,45,45,23,54,23,43));
model.addAttribute("listOfAge", listOfAge);
then try this:
<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>
来源:https://stackoverflow.com/questions/35678403/spring-mvc-show-data-in-a-table-row