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

问题描述 投票:0回答:1

我有一个网络应用程序使用带有百里香的引导程序形式:

<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.

如何解决这个问题?

spring spring-mvc thymeleaf spring-thymeleaf
1个回答
0
投票

看来我已经解决了!

https://github.com/thymeleaf/thymeleaf-spring/issues/222

我将此代码添加到我的 SecurityConfig.class 方法中:

public SecurityFilterChain filterChain(HttpSecurity http) {
    http.sessionManagement(httpSecuritySessionManagementConfigurer -> httpSecuritySessionManagementConfigurer.sessionCreationPolicy(SessionCreationPolicy.ALWAYS));
}

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