Spring Boot 当我在Thymeleaf中的form标签中同时使用th:action和method属性时,它会给我异常

vkc1a9a2  于 2023-11-17  发布在  Spring
关注(0)|答案(3)|浏览(173)

这是非常奇怪的,当我在表单标签中的第:action和method属性时,它会给我异常,但是当我从表单标签中删除method属性时,却没有异常发生。

  1. <form id="deliveryAddressForm" action="#" th:action="@{/product/save-address}" method="post" th:object="${address}">

字符串

发生例外状况

  1. <form id="deliveryAddressForm" action="#" th:action="@{/product/save-address}" th:object="${address}">

删除method=“post”后无异常

我不明白它为什么会这样。

请查看下面随附的代码

  1. Html代码
  1. <form id="deliveryAddressForm" action="#" th:action="@{/product/save-address}" method="post" th:object="${address}">
  2. <!-- Invoice Address-->
  3. <h3 class="mb-4"> Invoice address</h3>
  4. <div class="row">
  5. <div class="form-group col-md-6">
  6. <label class="form-label" for="fullname_invoice">Full Name</label>
  7. <input class="form-control" type="text" th:field="*{name}" placeholder="Joe Black" id="fullname_invoice">
  8. </div>
  9. <div class="form-group col-md-6">
  10. <label class="form-label" for="emailaddress_invoice">Email Address</label>
  11. <input class="form-control" type="text" th:field="*{email}" placeholder="[email protected]" id="emailaddress_invoice">
  12. </div>
  13. <div class="form-group col-md-6">
  14. <label class="form-label" for="phonenumber_invoice">Phone Number</label>
  15. <input class="form-control" type="text" th:field="*{mobile}" placeholder="Phone Number" id="phonenumber_invoice">
  16. </div>
  17. <div class="form-group col-md-6">
  18. <label class="form-label" for="street_invoice">Street</label>
  19. <input class="form-control" type="text" th:field="*{street}" placeholder="123 Main St." id="street_invoice">
  20. </div>
  21. <div class="form-group col-md-6">
  22. <label class="form-label" for="city_invoice">City</label>
  23. <input class="form-control" type="text" th:field="*{city}" placeholder="City" id="city_invoice">
  24. </div>
  25. <div class="form-group col-md-6">
  26. <label class="form-label" for="zip_invoice">Pincode</label>
  27. <input class="form-control" type="text" th:field="*{pincode}" placeholder="Postal code" id="zip_invoice">
  28. </div>
  29. <div class="form-group col-md-6">
  30. <label class="form-label" for="state_invoice">Land Mark</label>
  31. <input class="form-control" type="text" th:field="*{landmark}" placeholder="State" id="state_invoice">
  32. </div>
  33. <div class="form-group col-12 mt-3">
  34. <div class="custom-control custom-checkbox">
  35. <input class="custom-control-input" id="show-shipping-address" type="checkbox" name="clothes-brand">
  36. <label class="custom-control-label align-middle" for="show-shipping-address" data-toggle="collapse" data-target="#shippingAddress">Use a different shipping address</label>
  37. </div>
  38. </div>
  39. </div>
  40. <!-- /Invoice Address-->
  41. <!-- Shippping Address-->
  42. <div class="collapse" id="shippingAddress">
  43. <h3 class="my-4">Shipping address </h3>
  44. <div class="row">
  45. <div class="form-group col-md-6">
  46. <label class="form-label" for="street_shipping">Street</label>
  47. <input class="form-control" type="text" th:field="*{street1}" placeholder="123 Main St." id="street_shipping">
  48. </div>
  49. <div class="form-group col-md-6">
  50. <label class="form-label" for="city_shipping">City</label>
  51. <input class="form-control" type="text" th:field="*{city1}" placeholder="City" id="city_shipping">
  52. </div>
  53. <div class="form-group col-md-6">
  54. <label class="form-label" for="zip_shipping">Pin</label>
  55. <input class="form-control" type="text" th:field="*{pincode1}" placeholder="Postal code" id="zip_shipping">
  56. </div>
  57. <div class="form-group col-md-6">
  58. <label class="form-label" for="state_shipping">Land Mark</label>
  59. <input class="form-control" type="text" th:field="*{landmark1}" placeholder="State" id="state_shipping">
  60. </div>
  61. <div class="form-group col-md-6">
  62. <label class="form-label" for="phonenumber_shipping">Phone Number</label>
  63. <input class="form-control" type="text" th:field="*{mobile1}" placeholder="Phone Number" id="phonenumber_shipping">
  64. </div>
  65. </div>
  66. </div>
  67. <!-- /Shipping Address-->
  68. <div class="my-5 d-flex justify-content-between flex-column flex-lg-row"><a class="btn btn-link text-muted" href="#"> <i class="fa fa-angle-left mr-2"></i>Back </a><a class="btn btn-dark forward-btn" data-pan="1" href="javascript:void(0)" id="saveDeliveryAddressForm">Choose delivery method<i class="fa fa-angle-right ml-2"></i></a></div>
  69. </form>


1.模型

  1. package com.ij.ikimall.bean;
  2. import org.springframework.stereotype.Component;
  3. import java.io.Serializable;
  4. @Component
  5. public class AddressBean implements Serializable {
  6. private String name;
  7. private String email;
  8. private String mobile;
  9. private String street;
  10. private String city;
  11. private String pincode;
  12. private String landmark;
  13. private String mobile1;
  14. private String street1;
  15. private String city1;
  16. private String pincode1;
  17. private String landmark1;
  18. public String getName() {
  19. return name;
  20. }
  21. public void setName(String name) {
  22. this.name = name;
  23. }
  24. public String getEmail() {
  25. return email;
  26. }
  27. public void setEmail(String email) {
  28. this.email = email;
  29. }
  30. public String getMobile() {
  31. return mobile;
  32. }
  33. public void setMobile(String mobile) {
  34. this.mobile = mobile;
  35. }
  36. public String getStreet() {
  37. return street;
  38. }
  39. public void setStreet(String street) {
  40. this.street = street;
  41. }
  42. public String getCity() {
  43. return city;
  44. }
  45. public void setCity(String city) {
  46. this.city = city;
  47. }
  48. public String getPincode() {
  49. return pincode;
  50. }
  51. public void setPincode(String pincode) {
  52. this.pincode = pincode;
  53. }
  54. public String getLandmark() {
  55. return landmark;
  56. }
  57. public void setLandmark(String landmark) {
  58. this.landmark = landmark;
  59. }
  60. public String getMobile1() {
  61. return mobile1;
  62. }
  63. public void setMobile1(String mobile1) {
  64. this.mobile1 = mobile1;
  65. }
  66. public String getStreet1() {
  67. return street1;
  68. }
  69. public void setStreet1(String street1) {
  70. this.street1 = street1;
  71. }
  72. public String getCity1() {
  73. return city1;
  74. }
  75. public void setCity1(String city1) {
  76. this.city1 = city1;
  77. }
  78. public String getPincode1() {
  79. return pincode1;
  80. }
  81. public void setPincode1(String pincode1) {
  82. this.pincode1 = pincode1;
  83. }
  84. public String getLandmark1() {
  85. return landmark1;
  86. }
  87. public void setLandmark1(String landmark1) {
  88. this.landmark1 = landmark1;
  89. }
  90. }


1.控制器

  1. @GetMapping("/checkout/{productEncId}")
  2. public String checkout(@PathVariable(value = "productEncId")String productEncId,Model model) {
  3. Product product = productService.findByEncryptedId(productEncId);
  4. model.addAttribute("productEncId",productEncId);
  5. AddressBean address = new AddressBean();
  6. model.addAttribute("address",address);
  7. return "view/checkout";
  8. }

另请查找生成的详细例外

  1. 2020-08-28 15:51:08.507 ERROR 20228 --- [nio-8989-exec-8] org.thymeleaf.TemplateEngine : [THYMELEAF][http-nio-8989-exec-8] Exception processing template "view/checkout": An error happened during template parsing (template: "class path resource [templates/view/checkout.html]")
  2. org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/view/checkout.html]")
  3. at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:241) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE]
  4. Caused by: org.attoparser.ParseException: Error during execution of processor 'org.thymeleaf.spring5.processor.SpringActionTagProcessor' (template: "view/checkout" - line 41, col 79)
  5. at org.attoparser.MarkupParser.parseDocument(MarkupParser.java:393) ~[attoparser-2.0.4.RELEASE.jar:2.0.4.RELEASE]
  6. at org.attoparser.MarkupParser.parse(MarkupParser.java:257) ~[attoparser-2.0.4.RELEASE.jar:2.0.4.RELEASE]
  7. at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:230) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE]
  8. Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring5.processor.SpringActionTagProcessor' (template: "view/checkout" - line 41, col 79)
  9. at org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:117) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE]
  10. at org.thymeleaf.processor.element.AbstractElementTagProcessor.process(AbstractElementTagProcessor.java:95) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE]
  11. Caused by: java.lang.IllegalStateException: Cannot create a session after the response has been committed
  12. at org.apache.catalina.connector.Request.doGetSession(Request.java:2999) ~[tomcat-embed-core-9.0.36.jar:9.0.36]
  13. at org.apache.catalina.connector.Request.getSession(Request.java:2441) ~[tomcat-embed-core-9.0.36.jar:9.0.36]
  14. 2020-08-28 15:51:08.524 ERROR 20228 --- [nio-8989-exec-8] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/view/checkout.html]")] with root cause
  15. java.lang.IllegalStateException: Cannot create a session after the response has been committed
  16. at org.apache.catalina.connector.Request.doGetSession(Request.java:2999) ~[tomcat-embed-core-9.0.36.jar:9.0.36]
  17. 2020-08-28 15:51:08.640 ERROR 20228 --- [nio-8989-exec-8] s.e.ErrorMvcAutoConfiguration$StaticView : Cannot render error page for request [/product/checkout/fvssrg5OLcM=] and exception [An error happened during template parsing (template: "class path resource [templates/view/checkout.html]")] as the response has already been committed. As a result, the response may have the wrong status code.

pobjuy32

pobjuy321#

SO上有多个问题解释了类似的问题,但除了这个答案here外,大多数问题都没有答案。
我在我的案例中进一步检查了这个问题,每次JSESSIONID没有手动设置或修改时,都会出现这个错误。
我做了一些小实验来弄清楚在什么情况下这个问题会消失:
1.使用会话创建策略ALWAYS可以通过始终生成会话来解决问题。

  1. .and()
  2. .sessionManagement()
  3. .sessionCreationPolicy(SessionCreationPolicy.ALWAYS)

字符串
1.使用csrf token repository(如Spring Security Issue 3906中所述)也有助于通过急切地创建csrf token来持久化csrf token(而不使用惰性持久化)。也可以使用CookieCsrfTokenRepository

  1. .csrf()
  2. .csrfTokenRepository(new HttpSessionCsrfTokenRepository())


对于匿名用户,应用程序仅在需要时生成会话,因此,在呈现表单时请求CSRF令牌可能会在某些情况下导致此问题,例如在长html文件中延迟呈现表单。

展开查看全部
az31mfrm

az31mfrm2#

Error says org.thymeleaf.exceptions.TemplateInputException:模板解析过程中出错(template:“class path resource [templates//view/checkout.html]”)
注意双重// before视图。尝试在return语句中删除/ before视图

d6kp6zgx

d6kp6zgx3#

基于@sbsatter的回答,我设法解决了我的问题。由于一些depricated标志,我不得不修改代码。对于Sping Boot 3.x,它看起来更像:

  1. http.authorizeHttpRequests(
  2. requests -> requests.anyRequest().permitAll())
  3. .sessionManagement(sm ->
  4. sm.sessionCreationPolicy(SessionCreationPolicy.ALWAYS)
  5. )
  6. .csrf(csrf ->
  7. csrf.csrfTokenRepository(new HttpSessionCsrfTokenRepository())
  8. );

字符串

相关问题