我有一个使用 Thymeleaf 作为模板解析器的 Spring Boot 应用程序,在从 NetBeans 进行调试时工作正常,但在运行其 .jar 时出现此错误:
解析模板“/theme/property”时出错,模板可能不存在或无法被任何配置的模板解析器访问
该应用程序在 SpringBootServletInitializer 的扩展中使用注释 @SpringBootApplication 设置为自动配置器。我没有在属性文件中设置任何 contextPath 。我正在使用 Thymeleaf 2.1.6 和 Spring 4 版本。 jar 是用 Maven 生成的。
经过一些研究,我发现在某些控制器中我传递了双斜杠,我已经解决了这个问题,但大多数页面仍然无法正常工作。
该控制器的工作原理:
@GetMapping("/{idweb}")
String frontEndHome(@PathVariable("idweb")Integer idweb, Model model){
...
return "theme/home";
将 return 语句设置为返回“/主题/主页”;不起作用。我猜是因为模板解析器正在接收双斜杠(//)。
其他控制器引发错误:
@GetMapping("/{idweb}/property")
String frontEndProperty(@PathVariable("idweb") Integer idweb, @RequestParam(value = "idproperty", required = false) Integer idproperty, Model model) throws Exception {
...
return "theme/property";
索引控制器也工作正常:
@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 文件中的应用程序,结果相同:
spring.thymeleaf.prefix=classpath:/templates/
所有 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
还有一些其他问题涉及同一问题,但没有一个解决方案适合我。任何帮助将非常感激。
Update
将 jar 部署到 Pivotal Web Services 中,整个网站工作正常,但不能使用 Boxfuse、Heroku 部署它或在本地运行 jar。因此,我猜问题的根源是一些错误的配置,Pivotal 系统会检测到并纠正它。*
*
PWS 未纠正配置问题。它会在运行应用程序之前解压您的 jar 文件,从而阻止双斜杠引起问题。 – 安迪·威尔金森