thymeleaf 相关问题

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

如何在通过 thymeleaf 模板创建的 PDF 文件中显示印度卢比 ($) 字体

我正在从 thymeleaf 模板创建 html 并使用它来创建 PDF 文件。我正在使用 html2pdf 库从 html 转换为 pdf 并在 maven pom.xml 文件中使用以下依赖项...

回答 1 投票 0

如何在使用 Spring Boot 时在 JavaScript 中使用 import

我一直在寻找一种在我的网页中使用 import Chart from 'chart.js/auto' 的方法。我目前正在使用 Spring Boot 和 Thymeleaf。 问题是,我尝试过的所有方法都不起作用。我有一个node_modu...

回答 1 投票 0

已编译和执行的javascript程序之间的区别

这我们可以称之为这个问题的深化。 我们仍在使用 Java 1.8 的 Spring Boot 项目中,问题是将链接问题中描述的逻辑应用到预先存在的程序中

回答 1 投票 0

Spring MVC 控制器和 Javascript 之间传递模型时出错

在使用 Java 1.8 的 Spring Boot 项目中,我很难在正确显示值的控制器和似乎不正确的前端部分之间进行正确的通信...

回答 1 投票 0

HTMX 点击“输入键”或其替代方法时触发事件来发布数据

我需要在单击 Enter 键时触发 ajax 调用。我尝试使用 我需要在单击 Enter 键时触发 ajax 调用。我尝试使用 <form class="align-items-center row" th:action="@{/addTask}" method="post" th:object="${formData}" hx-post="/todo/addTask" hx-include="#pageSize" hx-target="#result" hx-swap="innerHTML" hx-trigger="click[enterKey]"> <div class="col-auto"> <label>Item :</label> </div> <div class="col-auto"> <input id="task" type="text" class="form-control" placeholder="Tasks..." th:field="*{task}" /> </div> <div class="col-auto"> <input type="submit" class="btn btn-primary" /> </div> </form> 但是没有成功 我需要一个 HTMX 在单击 Enter 键时触发 ajax 调用 keyup[keyCode==13] 为我工作,此页面可以提供帮助:https://www.toptal.com/developers/keycode/enter。 本页描述了如何链接事件和属性,但实际上 htmx 验证错误可以改进;) https://htmx.org/attributes/hx-trigger/ --> 主要解释在这里。 https://htmx.org/examples/keyboard-shortcuts/ ---> 示例 另请注意,click只是鼠标单击,而keyup或keydown是其他事件,事件可以链接,或者可以按顺序或替代方式触发(请参阅上面文档中的,的作用) )

回答 1 投票 0

如何通过rest调用使用输入表单获取数据?

我想通过以下表格获取用户的姓名和电子邮件地址 我想通过以下表格获取用户的姓名和电子邮件地址 <!-- Contatct Us--> <section> <div class="grey-bg"> <div class="container p-y-40"> <h1>Contact Us</h1> <div class="row"> <div class="col-md-6"> <div class="form-horizontal contact-us" th:action="@{/contact-us}"> <div class="form-group"> <label for="FirstName" class="col-sm-3 control-label mandatory">Name</label> <div class="col-sm-9"> <input type="text" class="form-control" th:value="${name}" placeholder=""/> <input name="name" type="text" th:value="${name}" placeholder=""/> <span th:text = "${name}"/> </div> </div> <!-- --> <div class="form-group"> <label for="Email" class="col-sm-3 control-label mandatory">Email</label> <div class="col-sm-9"> <input type="email" class="form-control" th:value="${email}" placeholder=""/> <input type="email" name="email" class="form-control" th:value="${email}" placeholder=""/> </div> </div> <!-- --> <div class="form-group"> <label for="Telephone" class="col-sm-3 control-label">Telephone</label> <div class="col-sm-9"> <input type="text" class="form-control" th:value="${telephone}" placeholder=""/> </div> </div> <!-- --> <div class="form-group"> <label for="Message" class="col-sm-3 control-label ">Message</label> <div class="col-sm-9"> <textarea class="form-control" rows="4" th:value="${message}"> </textarea> </div> </div> <!-- --> <div class="form-group"> <div class="col-sm-9 col-sm-offset-3 col-md-offset-3"> <input type="button" value="Send" class="btn btn-primary btn-flat w-min-120" data-toggle="modal" data-target="#myModal" /> </div> </div> <!-- --> </div> </div> <div class="col-md-6"> <iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d63371.792586563766!2d79.85603378454613!3d6.922006503004973!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x0%3A0x5f6f0f981a359f05!2sDialog+Axiata+PLC!5e0!3m2!1sen!2slk!4v1503969814382" width="100%" height="350" frameborder="0" style="border:0" allowfullscreen="true"></iframe> </div> </div> </div> </div> </section> 我这里使用了html和thymeleaf。这是我的控制器。在控制器中,我想将参数传递给我从用户那里获取的 sendUserRegisterEmail 方法。 @Autowired private EmailService emailService; @ResponseBody @RequestMapping(value = "/contact-us", method = RequestMethod.POST) public String showContactForm(@RequestParam("name") String contactName, @RequestParam("email") String contactEmail) { emailService.sendUserRegisterEmail(contactName, contactEmail); return "index"; } 我也写了一个DTO类。 public class ContactUsDto { private String name; private String email; private String telephone; private String message; public ContactUsDto(String name, String email, String telephone, String message) { this.name = name; this.email = email; this.telephone = telephone; this.message = message; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } } 目前我没有收到任何错误。但邮件没有收到。我对发送电子邮件所用的方法没有任何问题。因为我在其他情况下也使用过它。唯一的问题是将表单数据传递给它。请帮我解决这个问题 @RequestParam 用于传递请求参数,例如 http://example.com/path?name=myName&email=myEmail 而 @RequestBody 用于获取请求的正文,在大多数情况下,REST 是 json (application/json) 正文。所以你的方法是 @ResponseBody @RequestMapping(value = "/contact-us", method = RequestMethod.POST) public String showContactForm(@RequestBody ContactUsDto contactUsDto ) { emailService.sendUserRegisterEmail(contactUsDto.getContactName(), contactUsDto.getContactEmail()); return "index"; } 像这样添加到您的输入属性名称 <input type="email" name="email" class="form-control" th:value="${email}" placeholder=""/> <input name="name" type="text" th:value="${name}" placeholder=""/> 然后再试一次。 <form action="" method="get" id="authForm"> <table> <tr> <td>Email: </td> <td><input type="text" id="emailInput" name="EMAIL" value="<%= email ? email : '' %>" size="30" maxlength="30"></td> </tr> <tr> <td>Clave: </td> <td><input type="text" id="keyInput" name="KEY" value="" size="40" maxlength="40"></td> </tr> <tr> <td colspan="2"><input type="submit" value="Enviar"></td> </tr> </table> </form> <script> document.getElementById('authForm').onsubmit = function() { var email = encodeURIComponent(document.getElementById('emailInput').value); var key = encodeURIComponent(document.getElementById('keyInput').value); // Si la clave está vacía, asigna 'error' if (!key) { key = 'error'; } this.action = `/iniciar/${email}/${key}`; }; </script>

回答 3 投票 0

thymeleaf:如何使用模型属性填充 th:field 值

id 是模型属性。我想将其存储为 customer.id。

回答 1 投票 0

通过 Thymeleaf 中的方法验证显示错误

我正在使用 Spring Data JPA 来建模和验证我的数据。在本例中,我得到了一个同时具有密码和确认字段的类: 公开课报名表{ 私有字符串密码; p...

回答 1 投票 0

为单个 Spring Boot 实例配置多个模板解析器

我正在尝试配置第二个模板解析器以供 Thymeleaf 使用。我还想要在模板文件夹下查找的默认解析器,但无论我尝试什么,最终都只会得到一个重新...

回答 2 投票 0

带有 thymeleaf 的 Spring boot 不起作用

POM 聚甲醛 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>test</groupId> <artifactId>test</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.2.6.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> 申请 @SpringBootApplication public class Application { public static void main(String args[]){ SpringApplication.run(Application.class); } } 控制器 @Controller @RequestMapping("/index") public class IndexController { @RequestMapping("/") String index(){ return "index"; } } 我在资源文件夹和 error.html 和 index.html 中也有 templates 文件夹 当我访问 localhost:8080/index 时,显示错误而不是索引。我究竟做错了什么?这确实是最简单的设置,但它已经错了...... 问题出在你的IndexController课程上。按照您声明的方式,您可以通过 : http://localhost:8080/index/ 访问您的页面,但不能通过 localhost:8080/index url 访问您的页面。这就是为什么你的方法上有类注释 @RequestMapping 和相同的注释。 url 构造是某种层次结构 - 首先是 spring 检查类注释,然后是方法注释和构建的请求映射处理程序是: host + port + "index" + "/" 。 我又提出了一个潜在的问题。就我而言,直到 index.html 位于“static”文件夹中时它才起作用。当我将文件移动到“模板”文件夹时它起作用了。

回答 2 投票 0

如何在 Thymeleaf 中获取分数结果而不进行四舍五入

我试图从图像中获取高度和宽度之间的关系,然后乘以固定宽度以使用 Thymeleaf 调整图片大小。 为此,我将高度和宽度保存在 java 对象中

回答 2 投票 0

在 Thymeleaf 和 spring MVC 中提交表单上发布对象列表

我在后端有一个名为 PatientDto 的类。 @数据 @AllArgsConstructor @NoArgs构造函数 公共类 PatientDto { 私有字符串nationalId; 私有字符串名字; 私人

回答 1 投票 0

springboot3.2.4 + thymeleaf +AdminLTE,为什么找不到css?

这是资源文件目录 这是登录.html <...

回答 1 投票 0

当我在代码中使用 thymeleaf 并将标头代码替换为单独的文件时,然后不显示标头选项卡

我正在学习 Spring Boot 并在我的项目中使用 Thymeleaf。当我将标头代码替换为同一文件夹中的单独文件并使用 Thymeleaf 的替换语法、CSS、JS 和 Bootstrap 代码时...

回答 1 投票 0

Thymeleaf 枚举重构问题

我在重构模板时遇到了困难。我在一个模板中多次引用了有限数量的枚举值。 (在代码片段中,为了清楚起见,我仅指示了一小部分) ...

回答 1 投票 0

如何为 Web 应用程序设置 Thymeleaf?

我正在使用 eclipse 开发一个 Web 应用程序,我想使用 Thymeleaf,特别是我想从 servlet 设置一些变量,然后在 HTML 文件中使用。 我必须下载什么才能运行 Thymeleaf

回答 1 投票 0

百里香叶+春天。提交响应后无法创建会话

我有一个网络应用程序使用带有 thymeleaf 的引导程序形式: 我有一个网络应用程序使用带有百里香的引导程序形式: <form th:method="POST" th:action="@{/main}" th:object="${order}"> <div class="mb-3"> <label for="companyNameInput" class="form-label">Company Name</label> <input type="text" th:field="*{companyName}" class="form-control" id="companyNameInput" aria-describedby="companyHelp"> <div id="companyHelp" class="form-text">What is the name of your company?</div> <div style="color:red" th:if="${#fields.hasErrors('companyName')}" th:errors="*{companyName}">IF::ERROR</div> </div> <div class="mb-3"> <label for="contactPersonInput" class="form-label">Contact Person</label> <input type="text" th:field="*{contactName}" class="form-control" id="contactPersonInput" aria-describedby="nameHelp"> <div id="nameHelp" class="form-text">What is the name of the person we are going to talk to?</div> <div style="color:red" th:if="${#fields.hasErrors('contactName')}" th:errors="*{contactName}">IF::ERROR</div> </div> <div class="mb-3"> <label for="contactNumberInput" class="form-label">Number</label> <input type="text" th:field="*{number}" class="form-control" id="contactnumberInput" aria-describedby="numberHelp"> <div id="numberHelp" class="form-text">Leave us your number so we can contact with you to negotiate next steps!</div> <div style="color:red" th:if="${#fields.hasErrors('number')}" th:errors="*{number}">IF::ERROR</div> </div> <div class="mb-3"> <label for="contactEmailInput" class="form-label">Email</label> <input type="email" th:field="*{email}" class="form-control" id="contactEmailInput" aria-describedby="emailHelp"> <div id="emailHelp" class="form-text">We'll sent you an email to recall about your order when it's ready to go. We'll never share your email with anyone else.</div> <div style="color:red" th:if="${#fields.hasErrors('email')}" th:errors="*{email}">IF::ERROR</div> </div> <div class="mb-3"> <label for="contactAdressInput" class="form-label">Address</label> <input type="text" th:field="*{address}" class="form-control" id="contactAdressInput" aria-describedby="addressHelp"> <div id="addressHelp" class="form-text">Where would you like to get your order?</div> <div style="color:red" th:if="${#fields.hasErrors('address')}" th:errors="*{address}">IF::ERROR</div> </div> <div class="mb-3"> <label for="categorySelect" class="form-label">Product Category</label> <select class="form-select" th:field="*{category}" aria-label="categorySelect" id="categorySelect" aria-describedby="categoryHelp"> <option th:value="Cigarettes" selected>Cigarettes</option> <option th:value="Vapes">Vapes</option> <option th:value="Devices">Smoking Devices</option> <option th:value="Drinks">Drinks</option> <option th:value="Food">Food</option> </select> <div id="categoryHelp" class="form-text">Where would you like to get your order?</div> <div style="color:red" th:if="${#fields.hasErrors('category')}" th:errors="*{category}">IF::ERROR</div> </div> <div class="mb-3"> <label for="additionalInformationTextarea" class="form-label">Additional Information</label> <textarea class="form-control" th:field="*{additionalInformation}" id="additionalInformationTextarea" rows="5"></textarea> <div style="color:red" th:if="${#fields.hasErrors('additionalInformation')}" th:errors="*{additionalInformation}">IF::ERROR</div> </div> <div class="mb-3 form-check"> <input type="checkbox" class="form-check-input js-checkbox" id="privacyCheck"> <label class="form-check-label" for="privacyCheck">I agree to all the company's terms and conditions and the data processing rule.</label> </div> <button type="submit" class="btn btn-primary js-button" disabled>Place an order</button> </form> 这个控制器可以处理这个: @GetMapping("/main") public String mainPage(@ModelAttribute("order") Order order) { return "main"; } @PostMapping("/main") public String createOrder(@ModelAttribute("order") @Valid Order order, BindingResult bindingResult) { if (bindingResult.hasErrors()) { return "main"; } order.setStatus(String.valueOf(OrderStatus.Pending)); orderDetailsService.saveOrder(order); return "redirect:/main"; } 但是当我进入我的网站时,我遇到了这个异常,看起来我的网站有点冻结(就像手风琴或轮播不起作用)。但如果我从 html 代码中删除“表单”,网站将再次运行。如果我多次重新加载页面,则可能会显示表单并且网站会停止冻结。 2024-05-13T19:34:49.173+03:00 INFO 13472 --- [retailShop] [nio-8080-exec-2] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet' 2024-05-13T19:34:49.173+03:00 INFO 13472 --- [retailShop] [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet' 2024-05-13T19:34:49.175+03:00 INFO 13472 --- [retailShop] [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet : Completed initialization in 2 ms 2024-05-13T19:34:49.432+03:00 ERROR 13472 --- [retailShop] [nio-8080-exec-2] org.thymeleaf.TemplateEngine : [THYMELEAF][http-nio-8080-exec-2] Exception processing template "main": Error during execution of processor 'org.thymeleaf.spring6.processor.SpringActionTagProcessor' (template: "main" - line 383, col 44) org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring6.processor.SpringActionTagProcessor' (template: "main" - line 383, col 44) at org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:117) ~[thymeleaf-3.1.2.RELEASE.jar:3.1.2.RELEASE] 2024-05-13T19:34:49.438+03:00 ERROR 13472 --- [retailShop] [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring6.processor.SpringActionTagProcessor' (template: "main" - line 383, col 44)] with root cause java.lang.IllegalStateException: Cannot create a session after the response has been committed 2024-05-13T19:34:49.453+03:00 ERROR 13472 --- [retailShop] [nio-8080-exec-2] s.e.ErrorMvcAutoConfiguration$StaticView : Cannot render error page for request [/main] as the response has already been committed. As a result, the response may have the wrong status code. 如何解决这个问题? 看来我已经解决了! https://github.com/thymeleaf/thymeleaf-spring/issues/222 我将此代码添加到我的 SecurityConfig.class 方法中: public SecurityFilterChain filterChain(HttpSecurity http) { http.sessionManagement(httpSecuritySessionManagementConfigurer -> httpSecuritySessionManagementConfigurer.sessionCreationPolicy(SessionCreationPolicy.ALWAYS)); }

回答 1 投票 0

是否可以在thyemleaf中选择性地使用片段?

在SpringBoot3中,我用这样的片段制作了一段代码。 我想选择性地使用它。例如,...

回答 1 投票 0

Spring boot中前端向Controller传递参数

我正在创建一个用于预订航班的 Spring boot 应用程序。我有一个主页,其中航班详细信息使用 thymeleaf 显示在表格中,如下所示: &... 我正在创建一个用于预订航班的 Spring boot 应用程序。我有一个主页,其中航班详细信息使用 thymeleaf 显示在表格中,如下所示: <table border="solid"> <tr> <th>Airline</th> <th>Flight Number</th> <th>Departure</th> <th>Arrival</th> <th>Date</th> <th>Seats available</th> <th>Price</th> </tr> <tr th:each="flight : ${flightList}"> <td th:text="${flight.airlineName}"></td> <td th:text="${flight.flightNumber}" th:field="*{flightNumber}"></td> <td th:text="${flight.departure}"></td> <td th:text="${flight.arrival}"></td> <td th:text="${flight.date}"></td> <td th:text="${flight.availableSeats}"></td> <td th:text="${flight.price}"></td> </tr> </table> 下面是控制器: @Controller @RequestMapping("/booking") public class BookingController { @Autowired private FlightService flightService; @GetMapping("/booking-page") public String getBookingPage(@RequestParam("flightNumber") String flightNumber) { flightService.findFlightByFlightNumber(flightNumber); return "bookFlight"; } } 我正在尝试使用航班号获取特定航班(当单击特定行时)并将其作为参数传递给控制器,以便将其显示在下一页上。 但它不起作用。有什么替代方案吗? 以下是如何使用Model: @GetMapping("/booking-page") public String getBookingPage(@RequestParam("flightNumber") String flightNumber, Model model) { Flight flight = flightService.findFlightByFlightNumber(flightNumber); model.addAttribute("flight", flight); return "bookFlight"; } 或者,您可以使用 @ModelAttribute 直接返回航班对象作为模型属性: @GetMapping("/booking-page") public String getBookingPage(@RequestParam("flightNumber") String flightNumber, @ModelAttribute("flight") Flight flight) { flight = flightService.findFlightByFlightNumber(flightNumber); return "bookFlight"; }

回答 1 投票 0

TemplateEngine 未在模板文件夹中查找文件

我正在构建一个 SpringBoot 应用程序,它从数据库加载国际化消息。 (遵循本教程) 由此,我必须创建一个 ThymeleafConfiguration 类并设置一个 SpringTemplateEng...

回答 2 投票 0

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