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

标签 spring-mvc spring-boot thymeleaf

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

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

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

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

该 Controller 的工作原理是:

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

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

此其他 Controller 引发错误:
@GetMapping("/{idweb}/property")
String frontEndProperty(@PathVariable("idweb") Integer idweb, @RequestParam(value = "idproperty", required = false) Integer idproperty, Model model) throws Exception {
...
return "theme/property";

索引 Controller 也可以正常工作:
@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.htmlsrc/main/resources/templates/theme/home.htmlsrc/main/resources/templates/theme/property.html
还有其他一些问题可以解决相同的问题,但是没有一个解决方案适合我。任何帮助将非常感激。

更新

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

*
PWS无法纠正配置问题。它会在运行应用程序之前解压缩jar文件,从而阻止双斜杠引起问题。 – Andy Wilkinson

最佳答案

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

return "/theme/property"

代替:
return "theme/property"

就我而言,问题不在于 Controller ,而在于带有Thymeleaf片段引用的html中,如以下示例所示:
<footer th:replace="/index::footer"></footer>

代替:
<footer th:replace="index::footer"></footer>

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

关于spring-mvc - 使用打包在.jar中的Thymeleaf的Spring Boot错误解析模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48400967/

相关文章:

spring-mvc - 在 Spring Security 中传播 AccessDeniedException

Java Spring Mongo 排序忽略大小写问题

java - 无法为 Spring Boot Controller 创建 JUnit 测试

java - thymeleaf : How to use Custom Message Key in JSR-303 Annotation

java - 使用具有安全性的 spring mvc 时,为匿名用户存储一些数据的最佳方法是什么?

java - 处理两个 REST 端点读取相同数据时的延迟问题

java - 将随机端口暴露给 docker-compose.yml

java - Spring @Required 注解已弃用?

java - 解析 thymeleaf 文本模板中的特殊字符

java - 在java中使用thymeleaf html模板下载pdf文件时,css样式不可见