有没有办法将参数传递给Thymeleaf包括?

时间:2016-06-12 13:41:42

标签: thymeleaf

所以我有以下观点:

<body>
<!--@thymesVar id="repidTable" type="biz.tugay.RepidTypeTable"-->
<div th:include="repidTable"></div>
</body>

repidTable.html 如下:

<!--@thymesVar id="repidTypeColumns" type="java.util.List<biz.tugay.RepidTypeTableColumn>"-->
<!--@thymesVar id="repidTypeRows" type="java.util.List<biz.tugay.RepidTypeTableRow>"-->
<table>
    <thead>
    <tr>
        <th th:each="repidTypeColumn : ${repidTypeColumns}" th:text="${repidTypeColumn.columnHeaderText}">
        </th>
    </tr>
    </thead>
    <tr th:each="repidTypeRow : ${repidTypeRows}">
        <td th:each="repidTypeCell : ${repidTypeRow.repidTypeTableCells}" th:text="${repidTypeCell.data}">
        </td>
    </tr>
</table>

工作正常..但是,我希望能够将参数传递给 repidTable

我需要的是,而不是 repidTable.html ,例如 genericTable.html ,它将接受2个参数,我想传递repidTypeColumns和repidTypeRows,同时包括 genericTable.html

1 个答案:

答案 0 :(得分:1)

实际上这很容易。我有以下文件 messageFile.html

<!DOCTYPE html>
<html lang="en"
      xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Repid Type POC</title>
</head>
<body>
<!--@thymesVar id="messageObject" type="biz.tugay.Message"-->
<div th:fragment="messageDiv(messageObject)">
    <span th:text="${messageObject.getMessageBody()}"></span>
</div>
</body>
</html>

这是我的 index.html

<!DOCTYPE html>
<html lang="en"
      xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Repid Type POC</title>
</head>
<body>
<!--@thymesVar id="successMessage" type="biz.tugay.Message"-->
<!--@thymesVar id="errorMessage" type="biz.tugay.Message"-->
<div th:include="messageFile :: messageDiv(${successMessage})"></div>
<div th:include="messageFile :: messageDiv(${errorMessage})"></div>
</body>
</html>

我的控制器是这样的:

@Controller
@RequestMapping(value = "/")
public class HomeController {
    @RequestMapping(method = RequestMethod.GET)
    public String foo(Model model) {
        model.addAttribute("successMessage", new SuccessMessage());
        model.addAttribute("errorMessage", new ErrorMessage());
        return "index";
    }
}