【问题标题】:Spring MVC: Show data in a table row?Spring MVC:在表格行中显示数据?
【发布时间】:2016-06-11 05:51:03
【问题描述】:

我试图在table row 中显示data。但是它只是将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:(使用百里香叶)

<!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>

【问题讨论】:

  • http请求是什么样的?你能提供网址吗?如果 post 方法显示表单
  • 我从未使用过 tymeleaf,但如果它像 jsp 一样工作,我建议您检查包含内容。您的代码没问题,您的视图是 HTML 而不是 jsp 或服务器端处理的页面这一事实可能会导致使用 ${} 时出现问题。

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


【解决方案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>

【讨论】:

    猜你喜欢
    • 2016-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-03
    相关资源
    最近更新 更多