考虑到最新的 Spring Boot 版本 + 一些额外的 thymeleaf 模块,应用程序将不再在模板中生成 requestURI。我相信这与 thymeleaf 核心最近的变化有关(https://github.com/thymeleaf/thymeleaf/commit/dd01397c785d7d34049eecd67440aef52398599d)。但根据SO,这是正确的方法(https://stackoverflow.com/a/74922599/230638)。目前正确的做法是什么?
错误:
2023-10-13 14:44:20,158 ERROR [http-nio-8888-exec-1] [] o.a.c.c.C.[.[.[.[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/helpdesk/orders/list.html]")] with root cause
org.springframework.expression.EvaluationException: Accessing member 'requestUri' is forbidden for type 'class org.springframework.web.servlet.support.RequestContext' in this expression context
片段:
th:action="${#ctx.springRequestContext.requestUri}"
POM/依赖项:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.4</version>
</parent>
...
<dependency>
<groupId>nz.net.ultraq.thymeleaf</groupId>
<artifactId>thymeleaf-layout-dialect</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity6</artifactId>
</dependency>
Temurin JDK 17
3.1.4 版本对我来说也不起作用。但如果你降级到版本 3.1.1 就可以了。