使用打包在 .jar 中的 Thymeleaf 使用 Spring Boot 解析模板时出错 [英] Error resolving template with Spring Boot using Thymeleaf packaged in a .jar
问题描述
我有一个使用 Thymeleaf 作为模板解析器的 Spring Boot 应用程序,它在从 NetBeans 调试时工作正常,但在运行其 .jar 时出现此错误:
I have a Spring Boot application using Thymeleaf as template resolver, which works fine when debugging from NetBeans, but gives me this error running its .jar:
解析模板/theme/property"时出错,模板可能不存在或可能无法被任何配置的模板解析器访问
在 SpringBootServletInitializer 的扩展中,应用程序被设置为带有 @SpringBootApplication 注释的自动配置程序.我没有在属性文件中设置任何 contextPath.我正在使用 Thymeleaf 2.1.6 和 Spring 4 版本.jar 是用 Maven 生成的.
The app is set to auto-configurer with the annotation @SpringBootApplication, at an extension of SpringBootServletInitializer. I haven't set any contextPath into the properties file. I'm using Thymeleaf 2.1.6 and Spring 4 version. The jar is generated with Maven.
经过一些研究,我发现在某些控制器中我传递了一个双斜杠,我已经解决了这个问题,但大多数页面仍然无法正常工作.
Doing some research I've come out that in some controllers I was passing a double slash, which I've solved but most pages still not working.
此控制器有效:
@GetMapping("/{idweb}")
String frontEndHome(@PathVariable("idweb")Integer idweb, Model model){
...
return "theme/home";
将 return 语句设置为 return "/theme/home"; 不起作用.我猜是因为模板解析器收到双斜杠 (//).
With the return statement set as return "/theme/home"; doesn't work. I guess, because the template resolver is recieving a double slash (//).
另一个控制器引发错误:
This other controller raises the error:
@GetMapping("/{idweb}/property")
String frontEndProperty(@PathVariable("idweb") Integer idweb, @RequestParam(value = "idproperty", required = false) Integer idproperty, Model model) throws Exception {
...
return "theme/property";
索引控制器也能正常工作:
The index controller works fine as well:
@GetMapping("/")
public String index(Model model){
...
return "index";
}
这是我的应用入门类:
@SpringBootApplication
public class RentalWebsApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(RentalWebsApplication.class);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(RentalWebsApplication.class, args);
}
}
对于 Thymeleaf,我没有设置任何配置,尽管我已经测试了应用程序将其设置到 application.properties 文件中,结果相同:
For Thymeleaf I haven't set any configuration, although I've tested the app setting this into the application.properties file, with the same result:
spring.thymeleaf.prefix=classpath:/templates/
所有的html文件都设置为:
All html files are set into:
src/main/resources/templates
示例中的 html 文件位于:
The html files from the examples are in:
src/main/resources/templates/index.html
src/main/resources/templates/theme/home.html
src/main/resources/templates/theme/property.html
还有一些其他问题涉及同一问题,但没有一个解决方案适合我.任何帮助,将不胜感激.
There are some other questions dealing with the same issue, but none has a solution that works for me. Any help, would be much appreciated.
更新
将 jar 部署到 Pivotal Web Services 中,整个网站运行良好,但无法使用 Boxfuse、Heroku 进行部署或在本地运行 jar.因此,我猜问题的根源是一些错误的配置,Pivotal 系统检测到并更正了.*
Deploying the jar into Pivotal Web Services, the whole website works fine, but not deploying it with Boxfuse, Heroku or running the jar locally. Therefore, I guess the origin of the problem is some wrong configuration, that Pivotal system detects and corrects.*
*PWS 没有纠正配置问题.它会在运行应用程序之前解压您的 jar 文件,从而阻止双斜杠引起问题. – Andy Wilkinson一个>
推荐答案
最后的解决方案与双斜杠有关,如果我们设置 return 语句,classpath:/templates/
会得到以斜线开头,例如:
At the end the solution was related to the double slashes, that the classpath:/templates/
gets if we set a return statement with a slash at the beginning like:
return "/theme/property"
代替:
return "theme/property"
在我的例子中,问题不在于控制器,而在于带有 Thymeleaf 片段引用的 html,就像在这个例子中一样:
In my case, the problem was not at the controller, but in the html with the Thymeleaf references of fragments, like in this example:
<footer th:replace="/index::footer"></footer>
代替:
<footer th:replace="index::footer"></footer>
我不明白为什么 IDE(NetBeans 和 STS)不会引发错误.
What I don't understand is why the IDE's (NetBeans and STS), where not raising the error.
这篇关于使用打包在 .jar 中的 Thymeleaf 使用 Spring Boot 解析模板时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!