spring 如何让thymeleaf在我的.html文件中工作< iframe>?

f45qwnt8  于 2023-08-02  发布在  Spring
关注(0)|答案(1)|浏览(140)

我在HTML代码中使用了thymeleaf模板引擎。我的问题是,这在index.html iframe中工作,但在iframe中使用的其他文件中不工作。

index.html:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title></title>
    <link th:href="@{style_main.css}" rel="stylesheet" type="text/css">
    <script th:src="@{jsc/welcomeRandom.js}"></script>
    <script th:src="@{jsc/searchSys.js}"></script>
    <script th:src="@{jsc/btnsLogic.js}"></script>
    <script th:src="@{jsc/iframeUpdater.js}"></script>
    <script th:src="@{js/code.jquery.com_jquery-3.7.0.min.js}"></script>
</head>
<body class="mainBody">

        <div class="ssss">
            <p>ты че еблан?</p>
            <img th:src="@{icons/sss.jpg}" alt="eblan">
        </div>

        <div class="welcome" id="welcomeRandom">
        </div>

    <div class="app">

        <div class="sidebarFull" id="sidebar">
            <div class="logo" onclick="changeIframe('categories/home.html')">
                <img th:src="@{svg/logo.svg}" alt="logo">
                <p th:text="${test}"></p>
            </div>

            <div class="categories">
                <a tabindex="0" class="categoriesBtn" onclick="changeIframe('categories/minecraft.html')"><img th:src="@{icons/minecraft.png}" alt="icon">Minecraft</a>
                <a tabindex="0" class="categoriesBtn" onclick="changeIframe('categories/terraria.html')"><img th:src="@{icons/terraria.png}" alt="icon">Terraria</a>
                <a tabindex="0" class="categoriesBtn" onclick="changeIframe('categories/other.html')"><img th:src="@{icons/other.png}" alt="icon">Прочее</a>
            </div>

            <div class="addBtnDiv">
                <a tabindex="0" class="addBtn" onclick="changeIframe('categories/submission_page.html')">Добавить</a>
            </div>
        </div>

        <div class="articleFull">
            <div class="articleTrue">
                <div class="searchBarDiv">
                    <label for="searchInput"></label>
                    <input class="searchBar" id="searchInput" placeholder="Поиск по названию">
                </div>
                <div class="iframeContainer">
                    <iframe th:src="@{categories/minecraft.html}" class="iframeTrueContainer" id="iframeTrueContainerId"></iframe>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

字符串

minecraft.html:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>minecraft</title>
    <link href="../style_main.css" rel="stylesheet" type="text/css">
    <script src="../jsc/gridItemChecker.js"></script>
</head>
<body>
    <div class="homeDiv">

    </div>
    <div class="gridContent">
        <div class="gridItem" th:each="file : ${files}">
            <div><img src="@{file.iconpath}" alt="icon"></div>
            <div class="packNameDiv"><p class="packName" th:text="${file.name}"></p></div>
            <div><p class="sizeName">Размер</p><p class="sizeFact" th:text="${file.filesize}"></p></div>
            <div><p class="option1">FML</p><p class="option2" th:text="${file.option1}"></p></div>
            <div><p class="option1">Версия</p><p class="option2" th:text="${file.option2}"></p></div>
            <a class="gridItemBtn" href="#" tabindex="0">Скачать</a>
        </div>
    </div>
</body>
</html>

文件系统:

project file system
我希望thymeleaf标签能在我的iframe中使用的其他.html文件中工作,但相反,它们不工作,只是停留在页面的html代码中,而不是用所需的html代码替换它们自己。

1编辑:

<div class="iframeContainer">
                    <iframe th:src="/categories/minecraft" class="iframeTrueContainer" id="iframeTrueContainerId"></iframe>
</div>

MainController.java:

@Controller
public class MainController {
    UsersFilesController controller;

    @Autowired
    public MainController(UsersFilesController controller) {
        this.controller = controller;
    }

    @GetMapping("/")
    public String index(Model model){
        List<UsersFile> files = controller.getAll();
        model.addAttribute("files", files);
        System.out.println(model.getAttribute("files"));
        return "/main/index";
    }

    @GetMapping("/categories/minecraft")
    public String minecraft(Model model){
        return "categories/minecraft";
    }

}

ax6ht2ek

ax6ht2ek1#

您的Thymeleaf模板应该位于src/main/resources/templates文件夹中,以便使用默认设置找到。您甚至可以将它们放在像src/main/resources/templates/categories这样的子文件夹中。
如果您的项目不适合,您可以像这样更改application.properties中的默认设置:

spring.thymeleaf.prefix=classpath:/whatever/

字符串
iframeth:src的正确语法为:

<iframe th:src="@{/categories/minecraft}"/>


其中/categories/minecraft是控制器类中端点的名称。这是用@Controller注解的Java类。

相关问题