thymeleaf 相关问题

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

Spring MVC:如何在 Thymeleaf 中获取当前 url

我正在将 Thymeleaf 模板引擎与 Spring Web MVC 一起使用,并且在当前 url 的帮助下创建 url 时遇到了困难。有什么方法可以获取 Thymeleaf HTML 文件中的当前内容吗?例如:假设...

回答 5 投票 0

将用户名更改为电子邮件后无法进行身份验证。 Spring Boot 3 和 Spring Security 6

问题出在标题上。 安全配置 @配置 @EnableWebSecurity 公共类安全配置{ 私有最终 UserDetailsService 自定义UserDetailsService; 私人决赛

回答 1 投票 0

thymeleaf 模板类 - 无法解析为表达式

我有这个工作正常的模板: 我有这个工作正常的模板: <!DOCTYPE HTML> <html xmlns:th="http://www.thymeleaf.org" lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="x-ua-compatible" content="ie=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link rel="shortcut icon" th:href="@{/images/logo.png}" type="image/x-icon"/> <title>Mystic River</title> <link rel="stylesheet" th:href="@{/css/style.css}" /> <link rel="stylesheet" th:href="@{/css/responsive.css}" /> </head> <body> <!-- Header --> <header class="header-wrapper header_fixed bg_transparent" id="header-wrapper"> .... </html> 但是使用时 <!DOCTYPE HTML> <html xmlns:th="http://www.thymeleaf.org" lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="x-ua-compatible" content="ie=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link rel="shortcut icon" th:href="@{/images/logo.png}" type="image/x-icon"/> <title>Mystic River</title> <link rel="stylesheet" th:href="@{/css/style.css}" /> <link rel="stylesheet" th:href="@{/css/responsive.css}" /> </head> <body> <!-- Header --> <header th:class="header-wrapper header_fixed bg_transparent" id="header-wrapper"> 我有这个错误: Caused by: org.attoparser.ParseException: Could not parse as expression: "header-wrapper header_fixed bg_transparent" (template: "index" - line 14, col 13) at org.attoparser.MarkupParser.parseDocument(MarkupParser.java:393) at org.attoparser.MarkupParser.parse(MarkupParser.java:257) at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:230) ... 48 more Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as expression: "header-wrapper header_fixed bg_transparent" (template: "index" - line 14, col 13) at org.thymeleaf.standard.expression.StandardExpressionParser.parseExpression(StandardExpressionParser.java:131) at org.thymeleaf.standard.expression.StandardExpressionParser.parseExpression(StandardExpressionParser.java:62) at org.thymeleaf.standard.expression.StandardExpressionParser.parseExpression(StandardExpressionParser.java:44) 你有 class="header-wrapper header_fixed bg_transparent" 在第一个模板中并且 th:class="header-wrapper header_fixed bg_transparent" 在第二个(注意th:class而不是class)。 "header-wrapper header_fixed bg_transparent" 不是有效的 Thymeleaf 表达式。

回答 1 投票 0

如何在kotlin项目中的thymeleaf中获取枚举?

如何在kotlin项目中的thymeleaf中获取枚举? 这是一个枚举文件。 枚举类 SessionEnum(val displayValue: String) { 用户名(“用户名”) } 我

回答 1 投票 0

Thymeleaf th:文本属性值未显示在 Java 21 的 index.html 上

我正在使用 Thymeleaf 模板引擎来显示来自 Java 21 后端的一些信息。由于某些莫名其妙的原因,th:text 属性值没有显示在 html 页面上。非百里香叶

回答 1 投票 0

Thymeleaf + webauthn 客户端(浏览器)

我正在考虑构建一个 spring-thymeleaf 应用程序,该应用程序支持基于 webauthn 的密钥。 https://github.com/webauthn4j/webauthn4j-spring-security/tree/master/samples 显示服务器使用

回答 1 投票 0

thymeleaf 将输入和标签绑定到对象

鉴于将标签绑定到输入值是正确的,即: 你的名字 我想要

回答 1 投票 0

Thymeleaf 连接字符串

我有一个基本的 SpringBoot 应用程序。使用 Spring Initializer、嵌入式 Tomcat、Thymeleaf 模板引擎,并打包为可执行 JAR 文件 在其中 1 个模板中,我想连接 2 个值 &...

回答 2 投票 0

验证后百里香中的空文件

我有一个带有文件字段和复选框的百里香表单。两者都是必需的 我有一个带有文件字段和复选框的百里香表单。两者都是必需的 <form action="#" th:action="@{/upload}" th:object="${data}" method="post" enctype="multipart/form-data"> <input type="file" th:field="*{exampleFile}" accept="application/pdf"/> <div class="error" (...) /> <input type="checkbox" th:field="*{isOwner}"/> <div class="error" (...) /> (...) </form> 以及帖子方法 @PostMapping("upload") public String upload(@Valid @ModelAttribute("data") DataDTO data, BindingResult result, Model model) { if (result.hasErrors()) { return "upload"; } (...) return "redirect:/confirm"; } 当我选择文件但未选中复选框并提交表单时,复选框上会出现验证错误。 文件文件上没有验证错误(这是正确的),但此字段为空,我需要再次选择文件。 我需要表单在验证期间不删除文件 抱歉,我没有权限评论您的问题,我认为您的意思是类似this?

回答 1 投票 0

Thymeleaf 条件运算符给出错误,无法解析表达式

我是 Thyemleaf 的新手,面临无法解析表达式的问题 物品 是勒... 我是 Thyemleaf 的新手,面临无法解析表达式的问题 <p th:if="${modelMap.languages.size()&le; 2}" th:text="Items are less">Items are less 我想当列表的值小于2时在屏幕上打印特定消息 当我运行代码时,我收到以下消息 org.thymeleaf.exceptions.TemplateProcessingException:无法解析 作为表达式:“${modelMap.languages.size()} ≤ 2”(模板:“profile” - 第 32 行,第 5 栏)位于 org.thymeleaf.standard.expression.StandardExpressionParser.parseExpression(StandardExpressionParser.java:131) ~thymeleaf-3.1.2.RELEASE.jar:3.1.2.RELEASE] 在 org.thymeleaf.standard.expression.StandardExpressionParser.parseExpression(StandardExpressionParser.java:62) ~[thymeleaf-3.1.2.RELEASE.jar:3.1.2.RELEASE] 使用 le 代替 &le; 所以这应该有效: <span th:if="${modelMap.languages.size le 2}"> 2 or fewer languages </span> <span th:if="${modelMap.languages.size ge 3}"> 3 or more languages </span> 其他表达方式 lt = 小于 le = 小于或等于 gt = 大于 ge = 大于或等于 eq=等于 neq = 不等于

回答 1 投票 0

使用 J-Query 将新的输入字段添加到 Thymeleaf 表单

我正在尝试在 Spring 中创建一个应用程序,用户在其中设计一个测验。他们通过使用 Thymeleaf 表单提交问题来创建测验。 “问题”是一个具有一些不同属性的类...

回答 1 投票 0

如何使用Thymeleaf向html页面添加图像?

我的问题是我的 thymeleaf 块在 html 页面上不显示图像和快捷方式图标 我尝试使用文件路径: 我的问题是我的 thymeleaf 块在 html 页面上不显示图像和快捷方式图标 我用文件路径尝试过: <img class="logo" th:src="@{src/main/resources/static/logo.png}" alt="Logo"> 并且还尝试使用rest api: <img class="logo" th:src="@{/api/v1/logo}" alt="Logo"> 有控制器: @RestController @RequestMapping("/api/v1/logo") public class LogoController { @GetMapping(produces = MediaType.IMAGE_JPEG_VALUE) public Resource getLogo() throws IOException { return new ByteArrayResource(Files.toByteArray(new File("src/main/resources/static/logo.png"))); } } 我总是得到 alt 而不是图像... 问题1:正确读取文件 如果您使用默认配置,那么您放入 src/main/resources 的任何内容都会复制到类路径中。因此,您的代码中不应引用 src/main/resources,而应引用类路径本身。 所以理想情况下,您应该将控制器重写为: // Get location from classpath URI location = getClass().getClassLoader().getResource("static/logo.png").toURI(); // Get file Path file = Paths.get(location); // Read bytes return new ByteArrayResource(Files.readAllBytes(file)); 由于从文件中检索资源是一项常见任务,因此您实际上不必读取字节。 您可以使用 ByteArrayResource 而不是使用 FileSystemResource: // Get location from classpath URI location = getClass().getClassLoader().getResource("static/logo.png").toURI(); // Get file Path file = Paths.get(location); return new FileSystemResource(file); 您甚至可以缩短这个时间,因为从类路径检索资源非常常见,因此有一个 ClasspathResource 类: return new ClassPathResource("static/logo.png"); 这还不是全部,通常情况下,您需要从类路径提供 Web 资源,因此在 Spring Boot 中,classpath:static/ 文件夹或 classpath:public/ 文件夹中的所有内容都已在 Web 上提供。所以通常情况下,您的图片已经在 http://localhost:8080/logo.png 上可用。 所以通常你可以完全删除该控制器方法。 问题2:正确引用文件 这给我们带来了第二个问题。目前,您使用 @{/api/v1/logo} 或 @{src/main/resources/static/logo.png} 来引用您的图像。 Thymeleaf 将 @{path/to/file} 解释为上下文相关 URL,因此它唯一做的就是在上下文路径前面添加上下文路径(如果有的话),并期望该文件在 http://localhost:[serverport]/[contextpath]/path/to/file 处可用。 但正如我们之前所确定的,您的图像应该可以在 http://localhost:8080/logo.png 上找到,因此,您应该使用 @{/logo.png}: <img class="logo" th:src="@{/logo.png}" alt="Logo"> 如果这不起作用,那么: 您可能错误配置了构建工具,使其不在类路径中包含 src/main/resources。 您可能已将 Spring Boot 配置为不自动提供 classpath:static/ 或 classpath:public/ 中的任何内容。

回答 1 投票 0

无法使用 Spring Boot thymeleaf 加载图像

我对 Spring Boot 非常陌生,并且遵循 Guru Spring 框架教程。 我的简单 Spring Boot 应用程序运行成功,并且我的控制器工作正常。 我已经使用 thymeleaf 来显示 html 页面...

回答 7 投票 0

Thymeleaf 用链接代替按钮点击

我的模型定义了 ${entry.name} = 'MyClient' 我有一个带有 th:id="${entry.name}" 的按钮 我尝试使用链接来单击此按钮,按钮将被隐藏。 这个剪掉了...

回答 1 投票 0

如何使用 SpEL 处理 Thymeleaf 中的本地化消息

我是 ThymeLeaf 的初学者,除了 @PreAuthorize 注释之外没有太多使用 SpEL,所以请善意地帮助我。 我正在将 ThymeLeaf(版本 2.1.2)与 Spring 一起使用......

回答 4 投票 0

如何使用webpack 5向脚本标签添加nonce属性

我使用 webpack 5 和 HtmlWebpackPlugin 来构建我的前端 SPA。 我需要向 添加随机数属性 <question vote="0"> <p>我正在使用 webpack 5 和 <pre><code>HtmlWebpackPlugin</code></pre> 来构建我的前端 SPA。</p> <p>我需要将 <pre><code>nonce</code></pre> 属性添加到 <pre><code>&lt;script ...</code></pre> 注入的 <pre><code>HtmlWebpackPlugin</code></pre> 标签中。</p> <p>我该怎么做?</p> <p>额外问题:之后我在服务之前使用此页面作为 Thymeleaf 模板。如何注入<pre><code>nonce</code></pre>值?</p> </question> <answer tick="false" vote="0"> <p>如果您使用的是 webpack 4,海里有很多鱼——只需使用任何注入属性的插件,例如 <a href="https://github.com/numical/script-ext-html-webpack-plugin" rel="nofollow noreferrer">script-ext-html-webpack-plugin</a> 或 <a href="https://github.com/dyw934854565/html-webpack-inject-attributes-plugin" rel="nofollow noreferrer">html-webpack-inject-attributes-plugin</a> </p> <p>但是,上面提到的大多数插件都不适用于 webpack 5。解决方法是使用 <pre><code>HtmlWebpackPlugin</code></pre> <a href="https://github.com/jantimon/html-webpack-plugin#writing-your-own-templates" rel="nofollow noreferrer">templates</a>。</p> <ol> <li>将<pre><code>inject</code></pre>中指定的<pre><code>false</code></pre>选项中的<pre><code>HtmlWebpackPlugin</code></pre>设置为<pre><code>webpack.config</code></pre>。</li> <li>在模板中添加以下代码,将所有生成的脚本插入到结果文件中:</li> </ol> <pre><code> &lt;% for (key in htmlWebpackPlugin.files.js) { %&gt; &lt;script type=&#34;text/javascript&#34; defer=&#34;defer&#34; src=&#34;&lt;%= htmlWebpackPlugin.files.js[key] %&gt;&#34;&gt;&lt;/script&gt; &lt;% } %&gt; </code></pre> <ol start="3"> <li>在 <pre><code>script</code></pre> 中添加所有必要的属性。百里香示例:</li> </ol> <pre><code> &lt;% for (key in htmlWebpackPlugin.files.js) { %&gt; &lt;script type=&#34;text/javascript&#34; defer=&#34;defer&#34; th:attr=&#34;nonce=${cspNonce}&#34; src=&#34;&lt;%= htmlWebpackPlugin.files.js[key] %&gt;&#34;&gt;&lt;/script&gt; &lt;% } %&gt; </code></pre> <p>基于 <a href="https://github.com/jantimon/html-webpack-plugin/issues/538#issuecomment-270340587" rel="nofollow noreferrer">github 帖子</a></p>的答案 </answer> </body></html>

回答 0 投票 0

Spring Web 无法运行 Student/register.html

问题是尝试从控制器获取register.html,但我无法弄清楚? 这是 ThymeleafApplication.java 代码 包 com.cydeo.spring10thymeleaf; 导入org.springframework。

回答 1 投票 0

找不到模板位置:(请添加一些模板,检查你的 Thymeleaf 配置,或设置 spring.thymeleaf.check-template-location=false)

我创建了一个 Spring Boot 应用程序,在运行它时我收到此警告。 2022-10-27 00:35:12.520 警告 11512 --- [ restartedMain] ion$DefaultTemplateResolverConfiguration :找不到

回答 2 投票 0

Thymeleaf - 如何按索引循环列表

如何按索引循环? Foo.java 公共富{ 私人列表任务; ... } 索引.html 任务: 如何按索引循环? Foo.java public Foo { private List<String> tasks; ... } index.html <p>Tasks: <span th:each="${index: #numbers.sequence(0, ${foo.tasks.length})}"> <span th:text="${foo.tasks[index]}"></span> </span> </p> 我遇到解析错误 org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as each: "${index: #numbers.sequence(0, ${student.tasks.length})}" Thymeleaf th:each 允许您声明迭代状态变量 <span th:each="task,iter : ${foo.tasks}"> 然后在循环中可以参考iter.index和iter.size。 请参阅教程:使用 Thymeleaf - 6.2 保持迭代状态。 如果我们省略的话,Thymeleaf 总是声明隐式迭代状态变量。 <span th:each="task : ${foo.tasks}"> <span th:text="${taskStat.index} + ': ' + ${task.name}"></span> </span> 这里,状态变量名称为taskStat,是变量task和后缀Stat的聚合。 然后在循环中,我们可以引用taskStat.index、taskStat.size、taskStat.count、taskStat.even和taskStat.odd、taskStat.first和taskStat.last。 来源:教程:使用 Thymeleaf - 6.2 保持迭代状态 首先请原谅我糟糕的英语。 请注意,根据我的经验,关于 index 和 count 值的状态变量 ([1]) 不会跟踪迭代的状态,而是返回当前对象在迭代中的位置。列出您要循环播放的列表。 例如。 您有一个名为 pairs 的列表,其中有 6 对,如下所示 0: [id=10, fk=2] 1: [id=20, fk=2] 2: [id=30, fk=2] 3: [id=40, fk=1] 4: [id=50, fk=1] 5: [id=60, fk=1] 并且您想在具有 fk=1 的人之前打印具有 fk=2 的人(这是一个示例,此处排序并不重要)。 因此,如果您使用 th_each="p:${pairs}" 后跟 th:if="p.id == 1",您将获得 [id=40, fk=1] [id=50, fk=1] [id=60, fk=1] (...) 正如预期的那样。 但是,您对 pStat.index 和 pStat.count 值有何期望? 我猜 (0,1) (1,2) (2,3) 但是(请使用 <pre>([[${pStat.index}]], [[${pStat.count}]])</pre> 进行验证),您将获得 (3,4) (4,5) (5,6) 也就是说,这些对在列表中的位置,而不是迭代本身的状态。 这对您来说可能没问题,但请注意,例如,如果您尝试计算循环从一百个对象列表中提取数据的次数(例如每十个值插入一个 <br />)结果如果列表的顺序与您使用它的顺序不同,可能会让您感到惊讶。 如果是您的情况,获得此结果的一种方法是使用 Collection Selection ([2]) 结合 Thymeleaf 的 #lists.toList() 提取子列表,如下所示 <th:block th:with="sublist=${#pairs.toList(list.?[fk ==1])}">。 这样你将获得 0: [id=40, fk=1] 1: [id=50, fk=1] 2: [id=60, fk=1] 您的第一对将有索引 0 和计数 1。 也许这是我正在使用的 thymeleaf 当前版本的一个错误,或者在文档中没有明确说明,但这是我盯着几行代码意外行为(以及一些调试)发现的。 Thymeleaf 迭代 [1]:https://www.thymeleaf.org/doc/tutorials/3.1/usingthymeleaf.html#keeping-iteration-status SpEL 集合选择 [2]:https://docs.spring.io/spring-framework/reference/core/expressions/language-ref/collection-selection.html

回答 3 投票 0

如何告诉 Thymeleaf 填充密码字段?

我在 Spring Boot 应用程序中使用 Thymeleaf。以下表单工作正常,但如果表单的 dto 未通过验证,则不会填充密码字段: 我在 Spring Boot 应用程序中使用 Thymeleaf。以下表单工作正常,但如果表单的 dto 无法通过验证,则不会填充密码字段: <form id="registration-form" method="post" th:object="${registrationDto}" th:action="@{/registration}"> <input id="name" type="text" name="name" th:field="*{name}"/> <input id="password" type="password" name="password" th:field="*{password}"/> </form> 因此,当服务器端验证失败时,注册模板会再次显示,并且绑定到表单的registrationDto的属性已经设置(至少其中一个属性不正确)。 Thymeleaf 按预期填充文本字段,但 type="password" 的字段不会被预先填充。我尝试添加 th:value 但这没有帮助。 生成的 HTML 如下所示: <input id="password" type="password" name="password" value=""/> 仅供参考,密码本身会传递到表单,因为可以像这样获取它: <p th:text="*{password}"/> 因此我的问题是:如何让 Thymeleaf 也填充密码字段? 我目前得到的是以下解决方案: <form id="registration-form" method="post" th:object="${registrationDto}" th:action="@{/registration}"> <input id="name" type="text" name="name" th:field="*{name}"/> <input id="password" type="password" name="password" th:value="*{password}"/> </form> 这似乎有效,我相信,当您省略通过 th:field 显式指定字段时,Thymeleaf 将省略额外的检查。只要 name 属性与表单的 dto 属性正确匹配^^ Thymeleaf 仍然能够正确映射字段。 我不能说这是否是最好的解决方案,但至少它是可行的。

回答 1 投票 0

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