我正在使用 Thymeleaf 运行 Spring Boot 应用程序.当我通过 IDE (IntelliJ) 运行应用程序时,一切正常.但是,当我通过命令行 (java -jar) 运行应用程序时,视图无法解析,并且出现以下错误:
I am running a spring boot application with Thymeleaf. When I run the application through my IDE (IntelliJ) everything runs fine. However, when I run the application through the command line (java -jar) the views do not resolve and I get the following error:
org.thymeleaf.exceptions.TemplateInputException: Error resolving template "index", template might not exist or might not be accessible by any of the configured Template Resolvers at org.thymeleaf.TemplateRepository.getTemplate(TemplateRepository.java:245) at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1104) at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1060) at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1011) at org.thymeleaf.spring3.view.ThymeleafView.renderFragment(ThymeleafView.java:335) at org.thymeleaf.spring3.view.ThymeleafView.render(ThymeleafView.java:190) at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1225) at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1012) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)这是我的设置:
我的目录结构
PROJECT-ROOT --src --main --java --controllers --[CLASS WITH MAIN METHOD] --views --index.html我的模板解析器:
@Bean public ViewResolver viewResolver() { ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver(); templateResolver.setTemplateMode("XHTML"); templateResolver.setPrefix("views/"); templateResolver.setSuffix(".html"); SpringTemplateEngine engine = new SpringTemplateEngine(); engine.setTemplateResolver(templateResolver); ThymeleafViewResolver viewResolver = new ThymeleafViewResolver(); viewResolver.setTemplateEngine(engine); return viewResolver; }我应该把视图放在哪里,以便在从 jar 文件运行时可以正确解析它们?
Where should I put the views so that they can be correctly resolved when ran from a jar file?
推荐答案我认为答案取决于您的构建配置.目录src/main/views"不是任何常见构建工具的标准资源位置,因此您必须将其显式添加到用于构建 jar 的工具的配置中.
I think the answer is that it depends on your build configuration. The directory "src/main/views" is not a standard resource location for any common build tool, so you would have to explicitly add it to the configuration of the tool you use to build your jar.
如果我是你,我会顺其自然(为什么不同?),并且只使用src/main/resources"作为类路径资源.我也会完全省略 thymeleaf 配置,让 Spring Boot 处理它,将我的模板放在src/main/resources/templates"中.
If I were you I would go with the flow (why be different?), and just use "src/main/resources" for classpath resources. I would also leave out the thymeleaf configuration completely and let Spring Boot handle it, putting my templates in "src/main/resources/templates".
更多推荐
Thymeleaf 春季景观的正确位置
发布评论