thymeleaf 相关问题

Thymeleaf是一个XML / XHTML / HTML5模板引擎(可扩展到其他格式),可以在Web和非Web环境中工作。它更适合在Web应用程序的视图层提供XHTML / HTML5,它甚至可以在脱机环境中处理任何XML文件。它提供了一个可选模块,用于与Spring MVC集成,因此您可以在使用此技术的应用程序中将其用作JSP的完全替代,即使使用HTML5也是如此。

如何合并2个get请求?

我正在制作一个应用程序,标题中是一个搜索栏,您可以在其中写入一些内容,然后进行搜索并给出结果。 现在,在包含结果的页面上,我有另一个表格,您可以在其中

回答 1 投票 0

Spring Boot 中的国际化与 thymeleaf

我想用 thymeleaf 在 Spring Boot 中设置国际化。 我创建了这个类: @配置 公共类 MvcConfig 实现 WebMvcConfigurer { 私有最终消息源

回答 1 投票 0

在SpringBoot应用程序中设置WebSecurityConfig

我正在SpringBoot应用程序中设置WebSecurityConfig。 @配置 @EnableWebSecurity 公共类 WebSecurityConfig 扩展 WebSecurityConfigurerAdapter { .. } 但我有这个错误无法解决

回答 1 投票 0

Thymeleaf 如何查看当前 URL?

我以前很熟悉 JSTL,而 thymeleaf 对我来说是个新东西。 我想要的是检查当前 URL 是否是索引页 (/index),如果是,则使 div 可见。 这是 JSTL 等效项

回答 3 投票 0

Thymeleaf th:each - 如何禁用生成 div 标签

<div class="question" th:each="question : ${questions}"> <h2 id="question" class="description" th:text="${question.description}">Question goes here</h2> <div id="answer-buttons"> <button class="btn"><div class="answer" th:text="${question.possibleAnswers.get(0).content}" th:classappend="${question.correctAnswer == 1 ? 'correct-answer' : ''}">Answer 1</div></button> <button class="btn"><div class="answer" th:text="${question.possibleAnswers.get(1).content}" th:classappend="${question.correctAnswer == 2 ? 'correct-answer' : ''}">Answer 2</div></button> <button class="btn"><div class="answer" th:text="${question.possibleAnswers.get(2).content}" th:classappend="${question.correctAnswer == 3 ? 'correct-answer' : ''}">Answer 3</div></button> <button class="btn"><div class="answer" th:text="${question.possibleAnswers.get(3).content}" th:classappend="${question.correctAnswer == 4 ? 'correct-answer' : ''}">Answer 4</div></button> </div> <button id="next-btn">Next</button> </div> 如果我没猜错的话,上面代码中的th:each会为问题中的每个问题进行新的划分,类似于: <div class="quiz"> <div class="question"> <h2 id="question" class="description">Question</h2> <div id="answer-buttons"> <button class="btn"><div class="answer">Answer 1</div></button> <button class="btn"><div class="answer correct-answer">Answer 2</div></button> <button class="btn"><div class="answer">Answer 3</div></button> <button class="btn"><div class="answer">Answer 4</div></button> </div> <button id="next-btn">Next</button> </div> </div> 问题来自数据库,我有 js 文件来处理它们,但为了做到这一点,我不想创建额外的 html 正文。 所以我的问题是,Thymeleaf 是否有一些针对上述问题的禁用机制,或者我是否需要更改代码结构? 对于任何用途,以下是我的脚本处理数据的方式: const questionsFromDb = document.querySelectorAll('.question'); // Template const questions = [ { question: "Which is the largest animal on the planet?", answers: [ {text: "Shark", correct: false}, {text: "Blue whale", correct: true}, {text: "Elephant", correct: false}, {text: "Giraffe", correct: false} ] }, { question: "Which is the smallest country on the planet?", answers: [ {text: "Vatican City", correct: true}, {text: "Bhutan", correct: false}, {text: "Nepal", correct: false}, {text: "Shri Lanka", correct: false} ] }, { question: "Which is the largest desert on the planet?", answers: [ {text: "Kalahari", correct: false}, {text: "Gobi", correct: false}, {text: "Sahara", correct: false}, {text: "Antarctica", correct: true} ] }, { question: "Which is the smallest continent on the planet?", answers: [ {text: "Asia", correct: false}, {text: "Australia", correct: true}, {text: "Arctic", correct: false}, {text: "Africa", correct: false} ] } ]; // Parsing Data questionsFromDb.forEach(questionFromDb => { const questionDescription = questionFromDb.querySelector('.description').innerText; const answerElements = questionFromDb.querySelectorAll('.answer'); const answers = []; answerElements.forEach((answerElement, index) => { const answerText = answerElement.innerText; const isCorrect = answerElement.classList.contains('correct-answer'); answers.push({ text: answerText, correct: isCorrect }); }); questions.push({ question: questionDescription, answers: answers }); }); // ... Non-relative code below 添加新的 div 是一种有效的方法,你可以有额外的没有样式的 div,但是如果你想删除标签,你可以使用 th:remove="tag" 或 th:inline 机制,但这不是样式测试的好方法,尊重表达式语言方法,添加 html id 也不是正确的方法,您可以向 id 添加其他数据,例如:th:id="${'question_'+ status.id}" . 而且你的模板似乎是json! 如果你问我,我认为最好的方法是不要在这样的列表 ui 中使用 thymleaf,你可以使用 js 数据表,例如 tabulator 休息并玩 js 来代替

回答 1 投票 0

Thymeleaf 或 JSP:Spring Boot 哪个更好?

Thymeleaf 与 Spring Boot 结合比 JSP 更好吗? 他们的表现又如何呢? 是否有任何关于替代 UI 框架的建议 春季启动?

回答 1 投票 0

如何在 Thymeleaf 中格式化日期

我试过这个: 我试过这个: <p class="post-date"> <time datetime="2023-01-01" th:text="${#temporals.format(item.getCreatedDate(), 'dd-MM-yyyy HH:mm')}">Oct 5, 2023</time> </p> 但我收到此错误: 2024-03-30 17:15:58.620 ERROR [] o.a.c.c.C.[.[.[.[dispatcherServlet]@log(175) - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "#temporals.format(item.getCreatedDate(), 'dd-MM-yyyy HH:mm')" (template: "blog-details" - line 109, col 49)] with root cause org.springframework.expression.spel.SpelEvaluationException: EL1004E: Method call: Method format(java.sql.Timestamp,java.lang.String) cannot be found on type org.thymeleaf.expression.Temporals at org.springframework.expression.spel.ast.MethodReference.findAccessorForMethod(MethodReference.java:237) at org.springframework.expression.spel.ast.MethodReference.getValueInternal(MethodReference.java:147) at org.springframework.expression.spel.ast.MethodReference$MethodValueRef.getValue(MethodReference.java:400) 您的方法item.getCreatedDate()返回一个Instant。 这意味着一些事情: 这是一个可怕的名字。您确实应该重命名该方法。或者,让它返回一个 LocalDate,那么这个名字就可以了。日期不是时间戳。返回哪种时间概念的混乱是有害的:许多地方允许在实际上无法隐式转换的类型之间进行隐式转换,从而导致错误。您在测试中找不到的错误类型,因为它们只发生在时区或夏令时不匹配的情况下。 时间无法格式化Instant,因为你不应该这样做:瞬间是时间中的一个时刻,正如宇宙所测量的那样:它并不意味着代表、交互或适合任何基于人类的操作时间计算(天/月/年/小时/时区:这是人类计算。“自纪元以来的毫秒”,这是计算机计算)。例如,Instant 根本不编码时区。他们只是及时编码一个时刻。因此,用人类的语言来呈现它“不可能”!为此,您必须首先有一个时区; “那个瞬间是哪一个月?”无法回答。只有这样的问题:“阿姆斯特丹……的那个瞬间是哪一个月?” ——这个可以回答。您必须为其指定一个时区才能完成其工作 - 您可以通过 将您的 Instant 转换为例如一个ZonedDateTime。 does包含以人类计算术语呈现它所需的所有信息,并且事实上,时间的format可以处理ZDT(或LocalDateTime实例,您的选择)就好。 修复您的 getCreatedDate() 方法以返回更适合该作业的内容,或者使用以下方法将其返回的 Instant 转换为 ZDT:.getCreatedInstant().atZone(ZoneId.of("Europe/Amsterdam")); // or .getCreatedInstant().atZone(ZoneOffset.UTC);

回答 1 投票 0

CSS 应用于 Thymeleaf 中的一个端点,但不适用于另一个端点

<!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Flashcards</title> <link rel="stylesheet" th:href="@{css/style.css}"> </head> <body> <div class="game_container"> <!-- TITLE --> <h1>Flashcards</h1> <!-- CARDS CONTAINER --> <div class="cards_container"> <!-- FIRST CARD --> <div class="cards"> <div class="card"> <div class="front"><p>France</p></div> <div class="back"><p>Francoise Meyers</p></div> </div> </div> <!-- ...OTHER CARDS --> </div> </div> <!-- SCRIPT FOR CARD ROTATION --> <script> // Select all elements with the class "card" const cards = document.querySelectorAll('.card'); cards.forEach(card => { // Add event listener for click card.addEventListener('click', () => { // Toggle styles on click if (card.style.transform === 'rotateY(180deg)') { // If already rotated, revert back to original state card.style.transform = 'rotateY(0deg)'; } else { // Otherwise, rotate the card card.style.transform = 'rotateY(180deg)'; } }); }); </script> </body> </html> 如您所见,我在上面的 html 文件中为 css 提供了一些 href,它适用于默认端点 - localhost:8080。但是,如果我尝试指示自定义端点 ("api/v1/flashcards"),则不再应用 css,尽管抽认卡列表仍然以纯文本形式存在。这就是为什么我评论了@RequestMapping。 @RequiredArgsConstructor @Controller //@RequestMapping(path = "api/v1/flashcards") public class FlashcardController { private final FlashcardService flashcardService; public String listFlashcards(Model model) { List<FlashcardDto> flashcards = flashcardService.getFlashcards(); model.addAttribute("flashcards", flashcards); return "flashcards-list"; } } 我尝试将 @GetMapping(path = "api/v1/flashcards", produces = "text/html") 放在上面 listFlashcards()功能,但没有帮助。有什么想法吗? 我通过在 href 开头添加 / 来使用 css 文件的绝对路径。 之前: <link rel="stylesheet" href="css/style.css"> 之后: <link rel="stylesheet" href="/css/style.css"> 我不太明白为什么它会起作用。

回答 1 投票 0

th:样式在我的项目中不起作用,具体取决于情况 true:false

我的文本必须使用 Thymeleaf 根据 true:false 情况改变颜色,但无论我做什么都不起作用。 我的文本必须使用 Thymeleaf 根据 true:false 情况改变颜色,但无论我做什么,它都不起作用。 <table th:each="book : ${assignedBooks}"> <tr th:style="${book.getExpired()? 'color: red;' : 'color: blue;'}" th:text="${book.getName() + ', ' + book.getAuthor() + ', ' + book.getYear()}">'someBook'</tr> </table> 我已经检查了 .getExpired == true,但我不明白为什么它仍然不起作用。 您的 <table> HTML 包含一个或多个 <tr> 行 - 但这些行不包含任何 <td> 单元格。将文本(来自 th:text="...")放在 <td> 内。 <table th:each="book : ${assignedBooks}"> <tr th:style="${book.getExpired()? 'color: red;' : 'color: blue;'}"> <td th:text="${book.getName() + ', ' + book.getAuthor() + ', ' + book.getYear()}">'someBook'></td> </tr> </table> 您可以通过查看 Thymeleaf 生成并显示在浏览器中的 HTML 自行解决此类问题。请记住:Thymeleaf 在服务器上生成 HTML,然后将该 HTML 发送到浏览器。因此,您可以准确地看到 Thymeleaf 生成的内容(除非它在服务器上引发错误/异常)。 通常您的浏览器中有一个“查看源代码”菜单项(取决于您使用的浏览器)。 在您的情况下,使用问题中的方法,您可能会看到类似以下内容: The Adventures of Tom Sawyer, Mark Twain, 1876 <table> <tbody> <tr style="color: red;"></tr> </tbody> </table> 了解缺少任何 <td> 单元格如何导致文本呈现在表格之外。每个浏览器如何尝试渲染不完整/无效的 HTML 取决于每个浏览器 - 因此您可能会看到类似但不同的内容。 有关如何创建有效/完整 HTML 表格的更多详细信息,请参阅 table。

回答 1 投票 0

如何在 ThymeLeaf(非 Web 环境)中为类型注册格式化程序

我有一个基于SpringBoot的应用程序(SpringBoot 3.2.3)。该应用程序不是 Web 应用程序,即它不使用 servlet、视图等。 在应用程序中,我想以 HTML 格式创建报告

回答 1 投票 0

带有 Thymeleaf 的样式标签

我有这段代码: h1 {颜色...</desc> <question vote="0"> <p>我有这段代码:</p> <pre><code>&lt;h2 class=&#34;title&#34; &gt; &lt;a th:href=&#34;${blogItem.getLink()}&#34; th:text=&#34;${blogItem.getTitle()}&#34;&gt;&lt;style&gt;h1 {color:&#39;${blogItem.getTitleColor()}&#39;;} Title&lt;/style&gt;&lt;/a&gt; &lt;/h2&gt; </code></pre> <p>但是我有这个错误:<pre><code>Mismatched property value (&lt;color&gt; | [initial | inherit | unset | revert])</code></pre></p> </question> <answer tick="false" vote="0"> <p>假设您希望链接样式为标题,您可以用 <pre><code>&lt;h1&gt;</code></pre> 包围链接并使用 Thymeleaf 的 <pre><code>th:style</code></pre> 来实现您想要的:</p> <pre><code>&lt;h1&gt;&lt;a th:href=&#34;${blogItem.getLink()}&#34; th:text=&#34;${blogItem.getTitle()}&#34; th:style=&#34;&#39;color: &#39; + ${blogItem.getTitleColor()} + &#39;;&#39;&#34;&gt;Title&lt;/a&gt;&lt;/h1&gt; </code></pre> </answer> </body></html>

回答 0 投票 0

Thymeleaf 模板无法处理并抛出 NoSuchMethodError

我的 Thymeleaf 模板中的 th:href 属性遇到问题。我正在使用 Thymeleaf 版本 3.1.2.RELEASE 以及 Thymeleaf Spring5 集成。尽管设置了 th:href

回答 1 投票 0

我无法使用 Thymeleaf 重定向到 Spring Boot 3 中的更新页面

我遇到的问题是模板解析期间发生错误(模板:“类路径资源[templates/update-customer.html]”) org.thymeleaf.exceptions.TemplateInputException:呃...

回答 1 投票 0

Thymeleaf Security(秒:授权标签)在 Spring 中不起作用

我有一个问题:当我实现 thymeleaf 安全性并添加依赖项时,我注意到 sec:authorize 标签甚至不起作用! 我浏览了很多问题,但所有问题都已过时或被人伪造......

回答 1 投票 0

在 Spring Boot 中重定向到“/create”时出现问题

我有这个 ProductController.java 代码片段: @GetMapping("/创建") 公共字符串showCreatePage(模型模型){ ProductDto ProductDto = new ProductDto(); model.addAttribute(&...

回答 1 投票 0

使用布局方言时 Thymeleaf 和 Micronaut 视图错误

我们正在使用 Thymeleaf 视图和布局方言运行 Micronaut(我们通过覆盖 Micronaut 的 ThymeleafFactory 手动添加它)。以下是依赖项(Micronaut 版本为 3.2.7):

回答 3 投票 0

如何使用thymeleaf搜索并带回数据

[Thymeleaf] 如何将搜索数据传输到控制器 我正在进行搜索并带回结果,但我使用 th:value,它不起作用以及如何使用两个条件进行搜索。 <...

回答 1 投票 0

如果用户具有特定角色,如何附加CSS类?

仅当用户具有特定角色时,如何使用 th:classappend 将类附加到元素? 像这样的东西: 我正在使用

回答 3 投票 0

更新 Thymeleaf 中的内容,无需重新加载整个页面

我想更新div“output”的内容而不重新加载整个页面。当用户输入内容并点击运行按钮时,服务器将在 div 上输出评估“

回答 2 投票 0

Spring Boot 中 Thymeleaf 的几个模板位置

目前,Spring Boot 允许使用 spring.thymeleaf.prefix 属性为 Thymeleaf 模板位置使用一个值。 默认值为类路径:/templates/。 我想要另一个地点...

回答 2 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.