Java 无法使用thymeleaf运行spring启动应用程序
我的主要应用类:Java 无法使用thymeleaf运行spring启动应用程序,java,maven,spring-boot,thymeleaf,Java,Maven,Spring Boot,Thymeleaf,我的主要应用类: package com.sopra.springBoot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args){
package com.sopra.springBoot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args){
SpringApplication.run(Application.class, args);
}
}
我的控制器:
package com.sopra.springBoot;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HomeController {
@RequestMapping("/")
public String viewHome() {
return "welcome";
}
}
My welcome.html文件:位于/resources/templates/
请检查我哪里做错了。仍然无法显示welcome.html。
有关于自动配置的想法吗 您的代码在我的计算机上运行良好。我想这是因为你的 IntelliJ IDEA根据您的方式对类路径进行不同的排序 运行应用程序。在IDE中通过主应用程序运行应用程序 方法将导致在运行 应用程序使用Maven或Gradle,或从其打包的jar。这个可以 导致Spring引导在类路径上找不到模板。如果 受此问题的影响,您可以在 IDE将模块的类和资源放在第一位。或者, 您可以配置模板前缀以搜索每个模板 类路径上的目录:classpath*:/templates/ 这可以通过以下方式解决:
- 将资源文件夹添加到类路径中,请参见,然后在文件夹资源下创建名为application.properties的文件,将其放在config下面以包含所有类路径:
spring.thymeleaf.prefix=classpath*:/templates/ spring.thymeleaf.check-template-location=true spring.thymeleaf.suffix=.html spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.content-type=text/html spring.thymeleaf.mode=HTML5
- 通过执行
和mvn clean
运行项目mvn spring boot:run
.我知道这是第一个答案之后的3年。但是@xingbin的回答真的帮了我很大的忙 通过
mvn clean
和mvn spring boot:run
运行项目,确保有效
但是将上面的代码添加到application.properties中的类路径对我来说不起作用。所以我想,我只需要移除
spring.thymeleaf.prefix=classpath*:/templates/
这是我的密码:
spring.thymeleaf.check-template-location=true
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.cache=false
您是否仔细检查了
welcome.html
文件是否位于src/main/resources/templates
中?已检查,但仍然没有选择路径。我想还有别的东西丢失了关于日食氧气的任何想法?我的意思是如何设置类路径that@R.Sehdev试试这个,这应该是公认的答案,这是问题的原因。
2018-01-23 16:08:38.878 WARN 2904 --- [ restartedMain] o.s.b.a.t.ThymeleafAutoConfiguration : Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)
spring.thymeleaf.prefix=classpath*:/templates/
spring.thymeleaf.check-template-location=true
spring.thymeleaf.suffix=.html
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.check-template-location=true
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.cache=false