thymeleaf 相关问题

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

使用 Thymeleaf 将变量从 Java 传递到 Javascript

我在后端服务中有一个java Map>countryToExprMap。 在 Thymeleaf 渲染的网页中,我有一个“国家/地区”下拉菜单可供选择“国家/地区”,还有一个“Expr”下拉菜单,...

回答 1 投票 0

将鼠标悬停在链接上时显示工具提示

我有一个表格,显示有关音乐家的信息。前两行是流派和国家/地区。我使用 Thymleaf 模板引擎来渲染此类对象。这是我的代码: <... 我有一个表格,显示有关音乐家的信息。前两行是流派和国家/地区。我使用 Thymleaf 模板引擎来渲染此类对象。这是我的代码: <table> <tr> <td class="desc">Genres: </td> <td> <a class="genres" th:each="genre : ${genres}" th:href="${genre.getLink()}" th:text="${genre.getName()}" th:attr="data-description=${genre.getDescription()}"> </a> </td> </tr> <tr> <td class="desc">Countries: </td> <td> <a class="countries" th:each="country : ${countries}" th:href="${country.getLink()}" th:text="${country.getName()}" th:attr="data-description=${country.getDescription()}"> </a> </td> </tr> </table> 我还创建了一个将显示信息的块: <div id="description"></div> #description { background-color: white; border-radius: 10px; position: absolute; display: inline-block; opacity: 0; padding: 5px; transition: opacity 0.3s ease; font-size: small; max-width: 250px; } 我编写了 JS 代码来跟踪元素上的悬停和工具提示的显示: <script> document.addEventListener('DOMContentLoaded', function() { var genres = document.querySelectorAll('.genres'); var countries = document.querySelectorAll('.countries'); var Description = document.getElementById('description'); genres.forEach(function(genre) { var description = genre.getAttribute('data-description'); genre.addEventListener('mouseover', function() { Description.textContent = description; Description.style.opacity = "1"; var x = event.clientX/8 + 50; var y = event.clientY/2.5 - 5; Description.style.left = x + 'px'; Description.style.top = y + 'px'; }); genre.addEventListener('mouseout', function() { Description.style.opacity = "0"; }); }); countries.forEach(function(country) { var description = country.getAttribute('data-description'); country.addEventListener('mouseover', function() { Description.textContent = description; Description.style.opacity = "1"; var x = event.clientX - 1100; var y = event.clientY - 190; Description.style.left = x + 'px'; Description.style.top = y + 'px'; }); country.addEventListener('mouseout', function() { Description.style.opacity = "0"; }); }); }); </script> 一切似乎都正常,但有时国家/地区工具提示会出现故障。工具提示根本不会出现,而且国家本身也变得无法点击。也许我的代码没有优化?这种方法在多大程度上是正确的? 是的,您的代码可以改进,但目前它已经足够正确了。以下是我发现的问题和解决方案: /* change this */ var x = event.clientX - 1100; var y = event.clientY - 190; /* to this, */ // var x = event.clientX / 8 + 50; // var y = event.clientY / 2.5 - 5; /* or this */ // var x = event.clientX; // var y = event.clientY; [奖励] 另外,尝试将其添加到 #description 的样式中: pointer-events: none;

回答 1 投票 0

Spring thymeleaf 不会迭代列表

我正在尝试迭代列表并在表中显示结果,但表仍为空。 控制器 @RequestMapping(值=“/home”) public String home(Model model) 抛出 IOExce...

回答 2 投票 0

登录重新路由到登录?用户凭据正确时出现错误

我对编码相对较新,并且正在参与一个小组项目,以使用 Java MVC、Spring Boot、Thymeleaf 和 SQL 数据库进行额外练习。我们还使用 Spring Security。我的功能,用户

回答 1 投票 0

Thymeleaf 编码问题

我使用 Thymeleaf 3.0.3 和 Spring Boot 1.5.1 和 STS Bundle 3.8.1。我的匈牙利非 ASCII 字符未正确显示。 我在 application.properties 文件中有以下配置: ...

回答 3 投票 0

Thymeleaf webflux:仅允许将一个数据驱动变量指定为模型属性,但至少已识别两个

我正在尝试创建一个仪表板应用程序,其中多个小部件通过 SSE 获取更新。我的 DashboardController 看起来像: 公共类 DashboardController { 私有 WidgetService widgetSe...

回答 1 投票 0

我的 Spring 应用程序抛出 Whitelabel 错误页面而不是使用绑定结果

我正在尝试在实际培训中配置验证。但是,它没有在页面中向我显示错误消息,以便用户无法发送空或太大的文本,而是将我发送到 Whitel...

回答 1 投票 0

Thymeleaf 查询参数解析失败

我使用的是 Thymeleaf 版本 2.1.4,当将查询参数发送到 URL 时,我想打印字符串。它不是打印正确的字符串,而是像这样打印字符串对象 [Ljava.lang.String;@767a8ed5。我尝试...

回答 1 投票 0

简单 Spring thymeleaf + WebFlux 反应列表更改

目标:让我的 Web 应用程序在特定列表发生变化时做出反应。这些更改更新了 thymeleaf < p> html 元素。 问题:如果没有过度解决方案,该元素应该自动更新...

回答 1 投票 0

如何手动控制fields.hasErrors()和bindingResult

我正在尝试检查我是否有包含项目的列表。所以我在 HTML 中使用这段代码 ... 我正在尝试检查我是否有包含项目的列表。所以我在 HTML 中使用了这段代码 <div class="form-group-petit row"> <div class="col-sm-12"> <table class="table center" id="tableSelectedEstudis"> <col style="width:80%"> <col style="width:20%"> <!--<col style="width:10%">--> <thead> <tr> <th scope="col" data-th-text="#{edicio.estudis}"></th> <th scope="col" data-th-text="#{edicio.estudis.vigent}">Vigent</th> <!-- <th scope="col" data-th-text="#{label.accions}">Accions</th> --> </tr> </thead> <tbody> <tr th:each="estudi : *{listEstudis}" > <td scope="row" th:text="${estudi.codiEstudi +' - '+ estudi.memo}"/> <td scope="row" th:text="${estudi.vigentSN}"/> <!-- <td> <span class="link" th:attr="data-codiestudi =${estudi.codiEstudi}" id="eliminarEstudi" title="Elimina estudi" th:unless="*{altaOk} OR *{altaKo}"><i class="oi oi-delete"></i></span> </td> --> </tr> <tr></tr> </tbody> </table> </div> </div> <label class="error col-sm-10" th:if="${#fields.hasErrors('listEstudis')}" th:errors="*{listEstudis}"></label> 通常我应该在表单中添加@NonEmpty标签,让Spring自动工作。就我而言,我不能这样做,我需要手动添加错误。所以我在我的控制器中执行此操作: String[] codes = { "NotEmpty.admEdicionsDetallForm.listEstudis", "NotEmpty.listEstudis", "NotEmpty.java.util.List", "NotEmpty" }; String objectName = "admEdicionsDetallForm"; Object[] objects = { new DefaultMessageSourceResolvable( new String[] { "admEdicionsDetallForm.listEstudis", "listEstudis" }, null, "listEstudis") }; if (llistatEstudis.isEmpty()) { bindingResult.addError( new ObjectError(objectName, codes, objects, "És obligatori seleccionar almenys un estudi")); } 但是当我尝试手动执行此操作时,该消息并未显示,但是如果我使用 @NonEmpty 标签执行此操作,则它可以工作。 rejectValue()方法用于向BindingResult对象添加验证错误。 https://stackoverflow.com/a/65759773/2039546 所以,在你的代码中,而不是: bindingResult.addError( new ObjectError( objectName, codes, objects, "És obligatori seleccionar almenys un estudi")); 尝试: bindingResult.rejectValue( "listEstudis", "error.listEstudis", "És obligatori seleccionar almenys un estudi!");

回答 1 投票 0

Spring Boot 计算 SpringEL 表达式时出现异常

错误4904 --- [nio-8080-exec-1] org.thymeleaf.TemplateEngine : [THYMELEAF][http-nio-8080-exec-1]异常处理模板 “index”:模板解析时发生错误(模板:“cl...

回答 3 投票 0

验证 Thymeleaf 中的输入

我有这样的输入: 玛莎: 我有这个输入: Masa: <input type="number" class="form-control form-text" name="masa"/> <div class="text col-sm-12 error" th:if="${wzrost}" > <p class="text text-center"> To pole jest wymagane </p> </div> Wzrost: <input type="number" class="form-control form-text " name="wzrost"/> <div class="text col-sm-12 error" th:if="${wzrost}" > <p class="text text-center"> To pole jest wymagane </p> </div> 还有这个控制器; String x = String.valueOf(masa); String y = String.valueOf(wzrost); if(x==null ){ model.addAttribute("wzrost",true); return"views/success"; } if(y==null ){ model.addAttribute("wzrost",true); return"views/success"; } 当我单击表单提交按钮时,我总是收到错误 nullpointerexception。 如何验证输入,以便当它为空时弹出消息 @PostMapping("/cal-bmi") public String calculateBmiForm(Model model, Integer masa, Integer wzrost) { String x = String.valueOf(masa); String y = String.valueOf(wzrost); if(x==null ){ model.addAttribute("wzrost",true); return"views/success"; } if(y==null ){ model.addAttribute("wzrost",true); return"views/success"; } } 当我得到一个值表单 masa 和 wzrost 时,我从 null 进行检查,我单击“提交”,alwas nullpointerexception <form th:action="@{/cal-bmi}" method="post"> <ul class="gender-options"> <input id="man" type="radio" name="gender" value="male" required /> <label for="man">mężczyzna</label> &frasl; <input id="woman" type="radio" name="gender" value="female"/> <label for="woman">kobieta</label> </ul> Masa: <input type="number" class="form-control form-text" required placeholder="(kg)" name="masa"/> <!--<div class="text col-sm-12 error" th:if="${wzrost}">--> <!--<p class="text text-center">--> <!--To pole jest wymagane--> <!--</p>--> <!--</div>--> Wzrost: <input type="number" class="form-control form-text " required placeholder="(cm)" name="wzrost"/> <!--<div class="text col-sm-12 error" th:if="${wzrost}">--> <!--<p class="text text-center">--> <!--To pole jest wymagane--> <!--</p>--> <!--</div>--> <input type="submit" class="col-lg-10 btn btn-primary" value="Oblicz"/> </form> 现在我使用了必需的但不是很好的解决方案 您似乎想实现服务器端验证。为此,最好的方法是使用验证器及其绑定结果。实现服务器端验证的步骤是 有型号 public class PersonForm { private String name; public String getName() { return this.name; } public void setName(String name) { this.name = name; } } 在html中使用表单模型 <form action="#" th:action="@{/personForm}" th:object="${personForm}" method="post"> <table> <tr> <td><label th:text="#{label.name}+' :'"></label></td> <td><input type="text" th:field="*{name}" /></td> <td th:if="${#fields.hasErrors('name')}" th:errors="*{name}">Generic Error</td> </tr> <tr> <td><button type="submit">Submit</button></td> </tr> </table> </form> 有验证器类 @组件 public class PersonFormValidator implements Validator { @Override public boolean supports(Class<?> clazz) { return PersonForm.class.equals(clazz); } @Override public void validate(Object target, Errors errors) { ValidationUtils.rejectIfEmpty(errors, "name", "field.name.empty"); PersonForm p = (PersonForm) target; if (p.getName().equalsIgnoreCase("XXX")) { errors.rejectValue("name", "Name cannot be XXX"); } }} 将验证器绑定到控制器,让 spring 发挥魔法。 @控制器 public class WebController { @Autowired PersonFormValidator personFormValidator; @InitBinder("personForm") protected void initPersonFormBinder(WebDataBinder binder) { binder.addValidators(personFormValidator); } @PostMapping("/personForm") public String checkPersonInfo(@Validated PersonForm personForm, BindingResult bindingResult, final RedirectAttributes redirectAttributes) { if (bindingResult.hasErrors()) { return "personForm"; } redirectAttributes.addFlashAttribute("personResult", apiClientService.getPersonResult(personForm)); return "redirect:/spouseForm"; } }

回答 2 投票 0

使用 OGNL 在 Thymeleaf 中静态方法 Math#random 调用获取 OgnlException: getProperty(null, "lang") 的源为 null

我正在尝试 Thymeleaf 的非常基本的静态 Java 方法调用: 但总是遇到异常 OgnlException: source is null for 我正在尝试 Thymeleaf 进行非常基本的静态 Java 方法调用: <div th:text="${T(java.lang.Math).random()}"/> 但总是遇到例外OgnlException: source is null for getProperty(null, "lang")。 完整错误消息: 2023-08-11 16:17:16,221 ERROR [etp1561005241-55] o.t.TemplateEngine [THYMELEAF][etp1561005241-55] Exception processing template "test.html": Exception evaluating OGNL expression: "T(java.lang.Math).random( )" (template: "test.html" - line 89, col 14) org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating OGNL expression: "T(java.lang.Math).random()" (template: "diag-ui.html" - line 89, col 14) at org.thymeleaf.standard.expression.OGNLVariableExpressionEvaluator.evaluate(OGNLVariableExpressionEvaluator.java:199) at org.thymeleaf.standard.expression.OGNLVariableExpressionEvaluator.evaluate(OGNLVariableExpressionEvaluator.java:104) at org.thymeleaf.standard.expression.VariableExpression.executeVariableExpression(VariableExpression.java:166) at org.thymeleaf.standard.expression.SimpleExpression.executeSimple(SimpleExpression.java:66) at org.thymeleaf.standard.expression.Expression.execute(Expression.java:109) at org.thymeleaf.standard.expression.Expression.execute(Expression.java:138) at org.thymeleaf.standard.processor.AbstractStandardExpressionAttributeTagProcessor.doProcess(AbstractStandardExpressionAttributeTagProcessor.java:144) at org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:74) at org.thymeleaf.processor.element.AbstractElementTagProcessor.process(AbstractElementTagProcessor.java:95) at org.thymeleaf.util.ProcessorConfigurationUtils$ElementTagProcessorWrapper.process(ProcessorConfigurationUtils.java:633) at org.thymeleaf.engine.ProcessorTemplateHandler.handleStandaloneElement(ProcessorTemplateHandler.java:918) at org.thymeleaf.engine.StandaloneElementTag.beHandled(StandaloneElementTag.java:228) at org.thymeleaf.engine.TemplateModel.process(TemplateModel.java:136) at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:661) at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1103) at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1064) at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1053) at hobbit.mordor.diagui.DiaguiEndpoint.handleSearch(DiaguiEndpoint.java:106) at hobbit.mordor.diagui.DiaguiEndpoint.handle(DiaguiEndpoint.java:149) at hobbit.mordor.server.JettyRequestHandler.dispatchRequest(JettyRequestHandler.java:273) at hobbit.mordor.server.JettyRequestHandler.handle(JettyRequestHandler.java:313) at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:127) at org.eclipse.jetty.server.Server.handle(Server.java:516) at org.eclipse.jetty.server.HttpChannel.lambda$handle$1(HttpChannel.java:487) at org.eclipse.jetty.server.HttpChannel.dispatch(HttpChannel.java:732) at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:479) at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:277) at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:311) at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:105) at org.eclipse.jetty.io.ChannelEndPoint$1.run(ChannelEndPoint.java:104) at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136) at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) at java.base/java.lang.Thread.run(Thread.java:833) Caused by: ognl.OgnlException: source is null for getProperty(null, "lang") 感谢您的回答。 ${@java.lang.Math@random()}为我工作

回答 1 投票 0

如何在 Thymeleaf 3.0.5 中从数据库读取 Thymeleaf 模板?

我们正在从 Thymeleaf 2.1 升级到 3.0.5。我们当前的设置(升级之前)定义了许多 thymeleaf 模板并将其存储在数据库表中。 当我们尝试将 2.1 版本升级到 3.x 时...

回答 2 投票 0

Spring Security:我可以加载静态资源,但不能加载图像

我尝试了这里的所有建议,但无法使其发挥作用 Spring Boot安全静态资源 我必须指定我尝试添加的图像位于登录页面上。 我正在使用 Maven

回答 2 投票 0

Thymeleaf 文档中所需的字体未显示

我正在使用 Thymeleaf 模板生成 HTML 页面,然后将其用作电子邮件的内容。所需的字体系列是 Calibri。然而,当生成 HTML 文档时(然后...

回答 1 投票 0

如何在reactJS中使用Thymeleaf th:text

我正在使用Thymeleaf和reactJS运行一个springboot应用程序。所有 HTML 文本都是通过使用页面中的 th:text 从 message.properties 读取的,但是当我在 ReactJS HTML 块中有 th:text 时,重新...

回答 4 投票 0

日期字段未显示来自数据库的预填充日期

我面临一个可能与 HTML 和 Thymeleaf 有关的问题。我还附上了场景的屏幕截图。 当我

回答 1 投票 0

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

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

回答 1 投票 0

将作为模型传递的变量添加到th:onclick的location.href时发生错误

我尝试在 th:onclick 控制器中使用通过使用 model.addattribute() 获得的值,但出现错误。 // 正常工作 th:onclick="|location.href='/公告/' + ${list....

回答 0 投票 0

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