如何在我使用的<iframe>中让Thymeleaf在.html文件中工作?

huangapple go评论102阅读模式
英文:

How do I get thymeleaf to work in the .html files I use in my <iframe>?

问题

我在我的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>

我希望Thymeleaf标签能在其他用于iframe的.html文件中起作用,但实际上它们不起作用,只是保持在页面的HTML代码中,而不替换为所需的HTML代码。

英文:

I use the thymeleaf templating engine in my html code. My problem is that th works in the index.html iframe, but it doesn't work in other files that are used in the iframe.

index.html:

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot; xmlns:th=&quot;http://www.thymeleaf.org&quot;&gt;
&lt;head&gt;
&lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;title&gt;&lt;/title&gt;
&lt;link th:href=&quot;@{style_main.css}&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot;&gt;
&lt;script th:src=&quot;@{jsc/welcomeRandom.js}&quot;&gt;&lt;/script&gt;
&lt;script th:src=&quot;@{jsc/searchSys.js}&quot;&gt;&lt;/script&gt;
&lt;script th:src=&quot;@{jsc/btnsLogic.js}&quot;&gt;&lt;/script&gt;
&lt;script th:src=&quot;@{jsc/iframeUpdater.js}&quot;&gt;&lt;/script&gt;
&lt;script th:src=&quot;@{js/code.jquery.com_jquery-3.7.0.min.js}&quot;&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body class=&quot;mainBody&quot;&gt;
&lt;div class=&quot;ssss&quot;&gt;
&lt;p&gt;ты че еблан?&lt;/p&gt;
&lt;img th:src=&quot;@{icons/sss.jpg}&quot; alt=&quot;eblan&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;welcome&quot; id=&quot;welcomeRandom&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;app&quot;&gt;
&lt;div class=&quot;sidebarFull&quot; id=&quot;sidebar&quot;&gt;
&lt;div class=&quot;logo&quot; onclick=&quot;changeIframe(&#39;categories/home.html&#39;)&quot;&gt;
&lt;img th:src=&quot;@{svg/logo.svg}&quot; alt=&quot;logo&quot;&gt;
&lt;p th:text=&quot;${test}&quot;&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;categories&quot;&gt;
&lt;a tabindex=&quot;0&quot; class=&quot;categoriesBtn&quot; onclick=&quot;changeIframe(&#39;categories/minecraft.html&#39;)&quot;&gt;&lt;img th:src=&quot;@{icons/minecraft.png}&quot; alt=&quot;icon&quot;&gt;Minecraft&lt;/a&gt;
&lt;a tabindex=&quot;0&quot; class=&quot;categoriesBtn&quot; onclick=&quot;changeIframe(&#39;categories/terraria.html&#39;)&quot;&gt;&lt;img th:src=&quot;@{icons/terraria.png}&quot; alt=&quot;icon&quot;&gt;Terraria&lt;/a&gt;
&lt;a tabindex=&quot;0&quot; class=&quot;categoriesBtn&quot; onclick=&quot;changeIframe(&#39;categories/other.html&#39;)&quot;&gt;&lt;img th:src=&quot;@{icons/other.png}&quot; alt=&quot;icon&quot;&gt;Прочее&lt;/a&gt;
&lt;/div&gt;
&lt;div class=&quot;addBtnDiv&quot;&gt;
&lt;a tabindex=&quot;0&quot; class=&quot;addBtn&quot; onclick=&quot;changeIframe(&#39;categories/submission_page.html&#39;)&quot;&gt;Добавить&lt;/a&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;articleFull&quot;&gt;
&lt;div class=&quot;articleTrue&quot;&gt;
&lt;div class=&quot;searchBarDiv&quot;&gt;
&lt;label for=&quot;searchInput&quot;&gt;&lt;/label&gt;
&lt;input class=&quot;searchBar&quot; id=&quot;searchInput&quot; placeholder=&quot;Поиск по названию&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;iframeContainer&quot;&gt;
&lt;iframe th:src=&quot;@{categories/minecraft.html}&quot; class=&quot;iframeTrueContainer&quot; id=&quot;iframeTrueContainerId&quot;&gt;&lt;/iframe&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;

minecraft.html:

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot; xmlns:th=&quot;http://www.thymeleaf.org&quot;&gt;
&lt;head&gt;
&lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;title&gt;minecraft&lt;/title&gt;
&lt;link href=&quot;../style_main.css&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot;&gt;
&lt;script src=&quot;../jsc/gridItemChecker.js&quot;&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div class=&quot;homeDiv&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;gridContent&quot;&gt;
&lt;div class=&quot;gridItem&quot; th:each=&quot;file : ${files}&quot;&gt;
&lt;div&gt;&lt;img src=&quot;@{file.iconpath}&quot; alt=&quot;icon&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;packNameDiv&quot;&gt;&lt;p class=&quot;packName&quot; th:text=&quot;${file.name}&quot;&gt;&lt;/p&gt;&lt;/div&gt;
&lt;div&gt;&lt;p class=&quot;sizeName&quot;&gt;Размер&lt;/p&gt;&lt;p class=&quot;sizeFact&quot; th:text=&quot;${file.filesize}&quot;&gt;&lt;/p&gt;&lt;/div&gt;
&lt;div&gt;&lt;p class=&quot;option1&quot;&gt;FML&lt;/p&gt;&lt;p class=&quot;option2&quot; th:text=&quot;${file.option1}&quot;&gt;&lt;/p&gt;&lt;/div&gt;
&lt;div&gt;&lt;p class=&quot;option1&quot;&gt;Версия&lt;/p&gt;&lt;p class=&quot;option2&quot; th:text=&quot;${file.option2}&quot;&gt;&lt;/p&gt;&lt;/div&gt;
&lt;a class=&quot;gridItemBtn&quot; href=&quot;#&quot; tabindex=&quot;0&quot;&gt;Скачать&lt;/a&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;

file system:

project file system

I want the thymeleaf tags to work in the other .html files that are used in my iframes, but instead they don't work and just stay in the html code of the page instead of replacing themselves with the required html code.

1 EDIT:

&lt;div class=&quot;iframeContainer&quot;&gt;
&lt;iframe th:src=&quot;/categories/minecraft&quot; class=&quot;iframeTrueContainer&quot; id=&quot;iframeTrueContainerId&quot;&gt;&lt;/iframe&gt;
&lt;/div&gt;

MainController.java:

@Controller
public class MainController {
UsersFilesController controller;
@Autowired
public MainController(UsersFilesController controller) {
this.controller = controller;
}
@GetMapping(&quot;/&quot;)
public String index(Model model){
List&lt;UsersFile&gt; files = controller.getAll();
model.addAttribute(&quot;files&quot;, files);
System.out.println(model.getAttribute(&quot;files&quot;));
return &quot;/main/index&quot;;
}
@GetMapping(&quot;/categories/minecraft&quot;)
public String minecraft(Model model){
return &quot;categories/minecraft&quot;;
}
}

答案1

得分: 0

Your Thymeleaf templates should reside in the src/main/resources/templates folder to be found with the default settings. You can even put them in a subfolder like src/main/resources/templates/categories.

If it is not OK for your project you can change the default settings in the application.properties like this:

spring.thymeleaf.prefix=classpath:/whatever/

The correct syntax for th:src in iframe will be:

&lt;iframe th:src=&quot;@{/categories/minecraft}&quot;/&gt;

where /categories/minecraft is the name of the endpoint in the controller class. This is the Java class annotated with @Controller.

英文:

Your Thymeleaf templates should reside in the src/main/resources/templates folder to be found with the default settings. You can even put them in a subfolder like src/main/resources/templates/categories.

If it is not OK for your project you can change the default settings in the application.properties like this:

spring.thymeleaf.prefix=classpath:/whatever/

The correct syntax for th:src in iframe will be:

&lt;iframe th:src=&quot;@{/categories/minecraft}&quot;/&gt;

where /categories/minecraft is the name of the endpoint in the controller class. This is the Java class annotated with @Controller.

huangapple
  • 本文由 发表于 2023年7月24日 18:23:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76753540.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定