有没有办法将参数传递给 Thymeleaf 包含? [英] Is there a way to pass parameters to Thymeleaf includes?

查看:32
本文介绍了有没有办法将参数传递给 Thymeleaf 包含?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有以下观点:

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

repidTable.html如下:

<!--@thymesVar id="repidTypeRows" type="java.util.List<biz.tugay.RepidTypeTableRow>"--><表格><头><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>

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

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

解决方案

其实这很容易.我有以下文件 messageFile.html

<html lang="zh-cn"xmlns="http://www.w3.org/1999/xhtml"xmlns:th="http://www.thymeleaf.org"><头><title>Repid 类型 POC</title><身体><!--@thymesVar id="messageObject" type="biz.tugay.Message"--><div th:fragment="messageDiv(messageObject)"><span th:text="${messageObject.getMessageBody()}"></span>

</html>

这是我的index.html

<html lang="zh-cn"xmlns="http://www.w3.org/1999/xhtml"xmlns:th="http://www.thymeleaf.org"><头><title>Repid 类型 POC</title><身体><!--@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></html>

我的控制器是这样的:

@Controller@RequestMapping(value = "/")公共类 HomeController {@RequestMapping(method = RequestMethod.GET)公共字符串 foo(模型模型){model.addAttribute("successMessage", new SuccessMessage());model.addAttribute("errorMessage", new ErrorMessage());返回索引";}}

So I have the following views:

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

and repidTable.html is as follows:

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

which works fine.. However, I want to be able pass parameters to repidTable.

What I need is, instead of repidTable.html something like genericTable.html which will accept 2 parameters, and I would like to pass repidTypeColumns and repidTypeRows while including genericTable.html

解决方案

Actually this was quite easy. I have the following file 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>

and here is my 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>

and my controller is like this:

@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";
    }
}

这篇关于有没有办法将参数传递给 Thymeleaf 包含?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆