英文:
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:
<!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>
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:
<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";
}
}
答案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:
<iframe th:src="@{/categories/minecraft}"/>
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:
<iframe th:src="@{/categories/minecraft}"/>
where /categories/minecraft
is the name of the endpoint in the controller class. This is the Java class annotated with @Controller
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论