Spring MVC:在表格行中显示数据?

时间:2016-02-28 03:35:08

标签: html spring spring-mvc spring-boot html-table

我想在data中显示table row。然而,它只是将var name显示为输出,例如:

Name | age
User1| {age}

我正在使用Spring-MVCSpring 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>

1 个答案:

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