使用打包在.jar中的Thymeleaf的Spring Boot错误解析模板

Ale*_*ver 2 spring-mvc thymeleaf spring-boot

我有一个使用Thymeleaf作为模板解析器的Spring Boot应用程序,从NetBeans调试时可以正常工作,但是在运行其.jar时出现此错误:

错误解析模板“ / theme / property”,模板可能不存在,或者任何已配置的模板解析器都无法访问该模板

在SpringBootServletInitializer的扩展名下,该应用程序设置为使用@SpringBootApplication批注进行自动配置。我尚未在属性文件中设置任何contextPath。我正在使用Thymeleaf 2.1.6和Spring 4版本。这个罐子是用Maven生成的。

做一些研究,我发现在某些控制器中我传递了一个双斜杠,我已经解决了这个问题,但是大多数页面仍然无法正常工作。

该控制器的工作原理是:

@GetMapping("/{idweb}")
String frontEndHome(@PathVariable("idweb")Integer idweb, Model model){
...
return "theme/home";
Run Code Online (Sandbox Code Playgroud)

将return语句设置为return“ / theme / home”; 不起作用。我猜是因为模板解析器收到了双斜杠(//)。

此其他控制器引发错误:

@GetMapping("/{idweb}/property")
String frontEndProperty(@PathVariable("idweb") Integer idweb, @RequestParam(value = "idproperty", required = false) Integer idproperty, Model model) throws Exception {
...
return "theme/property";
Run Code Online (Sandbox Code Playgroud)

索引控制器也可以正常工作:

@GetMapping("/")
public String index(Model model){
   ...
   return "index";
}
Run Code Online (Sandbox Code Playgroud)

那是我的应用程序入门类:

@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);
    }
}
Run Code Online (Sandbox Code Playgroud)

对于Thymeleaf,我没有设置任何配置,尽管我已经测试了将其设置为application.properties文件的应用程序,但结果相同:

spring.thymeleaf.prefix=classpath:/templates/
Run Code Online (Sandbox Code Playgroud)

所有html文件都设置为:

src/main/resources/templates

示例中的html文件位于:

src/main/resources/templates/index.html

src/main/resources/templates/theme/home.html

src/main/resources/templates/theme/property.html

还有其他一些问题处理同一问题,但是没有一个问题对我有用。任何帮助将非常感激。

更新资料

将jar部署到Pivotal Web Services中,整个网站可以正常工作,但不能与Boxfuse,Heroku一起部署它,也不能在本地运行jar。因此,我认为问题的根源是Pivotal系统检测到并纠正了一些错误的配置。*

* PWS无法纠正配置问题。它会在运行应用程序之前解压缩jar文件,从而阻止双斜杠引起问题。安迪·威尔金森

Ale*_*ver 5

最后,解决方案与双斜杠有关,即classpath:/templates/如果我们在开始时设置带有斜杠的return语句,则得到的结果如下:

return "/theme/property"
Run Code Online (Sandbox Code Playgroud)

代替:

return "theme/property"
Run Code Online (Sandbox Code Playgroud)

就我而言,问题不在于控制器,而在于带有Thymeleaf片段引用的html中,如以下示例所示:

<footer th:replace="/index::footer"></footer>
Run Code Online (Sandbox Code Playgroud)

代替:

<footer th:replace="index::footer"></footer>
Run Code Online (Sandbox Code Playgroud)

我不明白的是为什么IDE(NetBeans和STS)没有引发错误。