Thymeleaf 春季景观的正确位置

编程入门 行业动态 更新时间:2024-10-25 02:19:08
本文介绍了Thymeleaf 春季景观的正确位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用 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 春季景观的正确位置

本文发布于:2023-10-12 01:26:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483363.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:景观   春季   正确   位置   Thymeleaf

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!