thymeleaf 相关问题

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

如何为 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

渲染从数据库 Spring Boot、Thymeleaf 检索到的 HTML

我有一个 Spring Boot 应用程序,我使用 TinyMCE 以 HTML 格式保存内容。 “内容”正确保存在数据库中...... 用于保存内容的 HTML 内容保存在数据库中 ...

回答 2 投票 0

在SpringBoot中验证表单输入-thymeleaf

我有这个控制器: @控制器 @Slf4j @RequestMapping("/natalchart") 公共类 NatalChartDataController { @GetMapping({"/data"}) 公共字符串数据(模型模型,

回答 1 投票 0

Spring Boot - GetMapping 到具有不同路径的相同方法的更简单方法

目前我的控制器看起来像这样。 @RequestMapping(“成员”) 公共类成员控制器{ 私有 ArrayList memberList = new ArrayList<>(); @GetMa...

回答 3 投票 0

注册后,将打开以下页面:https://myApp.by/assets/inputmask/jquery.inputmask.min.js

用户注册并登录后,打开以下页面: https://myApp.by/assets/inputmask/jquery.inputmask.min.js 在此输入图像描述 为什么当你第一次登录时它不会进入 h...

回答 1 投票 0

注册后,将打开以下页面:https://shtura.by/assets/inputmask/jquery.inputmask.min.js

用户注册并登录后,打开以下页面: https://shtura.by/assets/inputmask/jquery.inputmask.min.js 在此输入图像描述 据我了解,此页面仅打开一次...

回答 1 投票 0

springBoot + Thymeleaf:属性中的 UTF-8

我有这个配置文件: @配置 @EnableWebSecurity @EnableMethodSecurity(securedEnabled = true, jsr250Enabled = true) 公共类 MvcConfig 实现 WebMvcConfigurer { @豆 酒吧...

回答 1 投票 0

Thymeleaf:通过错误代码显示全局错误

如何在模板中按各自的错误代码显示多个全局错误? 当拒绝绑定结果时,第一个参数是错误代码。显示时如何使用它...

回答 3 投票 0

springBoot + Thymeleaf:验证电子邮件

我有这个对象: @Builder @数据 @AllArgsConstructor @NoArgs构造函数 公共类 NatalChartDataPayload { 字符串语言代码; @NotEmpty(message = "邮箱不能为空") @E...

回答 1 投票 0

在React中使用Thymeleaf变量

有没有办法在React中使用Thymeleaf变量? 我在这里读到这应该是一个坏主意。问题是,我使用 Thymeleaf 创建了一个 Spring Boot 应用程序,其中包含...

回答 1 投票 0

使用 thymeleaf 的 itext 生成 pdf

我想从我的模板thymeleaf生成pdf,因为我的目标是在我的模板中插入数据,然后生成pdf。但我不工作! TestController.java----------------------------------------...

回答 1 投票 0

在执行存储库保存方法之前,百里香表单中的对象被保存到数据库中

我有一个全栈应用程序,带有 thymeleaf 前端、spring 后端和 mariadb 数据库。当我尝试通过 thymeleaf 表单更新对象时,出现了奇怪的问题。 Obj 的类...

回答 1 投票 0

如何使用 Thymeleaf 和 Java Enum 检查所选选项

使用 Spring 和 Thymeleaf,我正在添加和编辑用户。我想对用户编辑页面上的选择标签进行选项检查。例如,如果用户的角色是 ADMIN,如果 admin 选项不是

回答 1 投票 0

出现错误:“小数值不正确:第 1 行‘价格’列的‘Apple Watch Series 6’。”价格设置了错误的值。我能做什么?

我正在用Java制作一个愿望清单网站,当我尝试更新愿望时,价格出现错误。我正在 thymeleaf 和 springboot 工作。 这是我的 html 表格(价格和描述/文本是

回答 1 投票 0

springBoot + Thymeleaf:使用 Lombok 读取值

我有这个豆子: @Builder @数据 @AllArgsConstructor @NoArgs构造函数 公共类 BlogSearchDataPayload { 字符串语言代码; String blogCategory = BlogCategoryEnum.all.name(); }

回答 1 投票 0

Thymeleaf:Sprint Boot 从 1.4.3 升级到 1.5.22 后,index.html 页面无法正确显示

最近,我开始尝试使用Thymeleaf升级SpringBoot中的一个应用程序(从1.4.3到1.5.22)。应用程序启动正常,但是当我检查这些 html 页面时,没有一个显示正确的...

回答 1 投票 0

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