Spring boot 弹簧靴筒';t解析thymeleaf模板
我不熟悉Spring5和SpringBoot。 我试图用thymeleaf创建一个Spring5/spring启动应用程序。 我想创建一个war,而不是使用带有spring boot的嵌入式Web服务器 当我部署war时,我的应用程序启动,我可以访问src/main/resources/static/中的测试html页面,其中包含调用我的控制器的javascript。我可以在这些页面上执行到控制器和数据库的往返 然而,当我试图打开位于src/main/resources/templates/testtemplate.html的thymeleaf页面时,我得到了404 相关专家:Spring boot 弹簧靴筒';t解析thymeleaf模板,spring-boot,thymeleaf,spring5,Spring Boot,Thymeleaf,Spring5,我不熟悉Spring5和SpringBoot。 我试图用thymeleaf创建一个Spring5/spring启动应用程序。 我想创建一个war,而不是使用带有spring boot的嵌入式Web服务器 当我部署war时,我的应用程序启动,我可以访问src/main/resources/static/中的测试html页面,其中包含调用我的控制器的javascript。我可以在这些页面上执行到控制器和数据库的往返 然而,当我试图打开位于src/main/resources/templates/te
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf-spring5 -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.11.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
<version>3.0.11.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.3.5.RELEASE</version>
</dependency>
网络配置:
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.myproject")
public class WebConfig implements WebMvcConfigurer
{
@Autowired
ApplicationContext ctx;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry)
{
registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
}
@Bean
@Description("Thymeleaf Template Resolver")
public SpringResourceTemplateResolver templateResolver()
{
SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver ();
templateResolver.setPrefix("/templates/");
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode(TemplateMode.HTML);
return templateResolver;
}
@Bean
@Description("Thymeleaf Template Engine")
public SpringTemplateEngine templateEngine()
{
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver());
templateEngine.setEnableSpringELCompiler(true);
templateEngine.setTemplateEngineMessageSource(messageSource());
return templateEngine;
}
@Bean
@Description("Thymeleaf View Resolver")
public ThymeleafViewResolver viewResolver()
{
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(templateEngine());
viewResolver.setOrder(1);
return viewResolver;
}
@Bean
@Description("Spring Message Resolver")
public ResourceBundleMessageSource messageSource()
{
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("messages");
return messageSource;
}
在web配置中,如果删除addResourceHandlers方法,则不会更改任何内容。我仍然可以在localhost:8080/上找到我的静态页面。
我尝试添加这一行:registry.addResourceHandler(“/**”).addResourceLocations(“classpath:/templates/”)代码>
当我这样做时,我可以访问localhost:8080/mytemplate.html上的thymileaf模板。但是,它显示为静态页面。这些片段没有翻译。“th”标记似乎被忽略
我还尝试从我的网络配置中删除templateResolver、viewResolver和templateEngine bean,因为我认为可能是覆盖了一些自动配置。这没有任何效果
我相信我的目录结构相当标准:
src/main/
/java/com/myproject/[code here]
/resources/static[web pages here]
/resources/templates[thymeleaf pages here]
我错过了什么?
我对现代春天完全是个新手。所以我可能在做些傻事。springboot中的所有这些自动配置都非常令人沮丧,因为我不知道如何调试它所做的工作。以下是如何通过一个简单的spring@Controller使用静态js/css资源为thymeleaf html提供服务
HTML-带有thymeleaf
homePage.html
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Hello App</title>
<!-- /resources/static/ folder is automatically mapped for static files -->
<script th:src="@{/js/app.js}"></script>
</head>
<body>
<h2 th:text="${msg}"></h2>
</body>
</html>
带控制器的Java
package demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@SpringBootApplication
public class WebApp {
public static void main(String[] args) {
SpringApplication.run(WebApp.class, args);
}
}
@Controller
class CtrlA {
@GetMapping({"", "/"})
String home(Model m) {
m.addAttribute("msg", "Hello World");
return "homePage";
}
}
目录结构:
alert("Hello Alert!")
├── pom.xml
├── src
│ └── main
│ ├── java
│ │ └── demo
│ │ ├── DemoApplication.java
│ │ └── WebApp.java
│ └── resources
│ ├── application.properties
│ ├── static
│ │ └── js
│ │ └── app.js
│ └── templates
│ └── homePage.html
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>gt</groupId>
<artifactId>web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
pom.xml:
alert("Hello Alert!")
├── pom.xml
├── src
│ └── main
│ ├── java
│ │ └── demo
│ │ ├── DemoApplication.java
│ │ └── WebApp.java
│ └── resources
│ ├── application.properties
│ ├── static
│ │ └── js
│ │ └── app.js
│ └── templates
│ └── homePage.html
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>gt</groupId>
<artifactId>web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
4.0.0
org.springframework.boot
spring启动程序父级
2.3.5.1发布
燃气轮机
网状物
0.0.1-快照
11
org.springframework.boot
弹簧启动装置
org.springframework.boot
SpringBootStarterWeb
org.springframework.boot
springbootmaven插件
我注意到您正在使用Spring Boot,但您正在定义自己的依赖版本和配置。添加spring boot starter thymeleaf
就足够了。请参考需要编译的模板。但看起来您正试图将它们作为静态HTML文件加载。@gitiwari333感谢您的回复。我尝试删除其他依赖项,但没有任何区别。我不明白你所说的“Thymeleaf模板需要编译”是什么意思?1)如果你打算使用真正的动态渲染(带有动态数据),我相信你必须使用Spring MVC。2) 如果您使用thymeleaf只是为了生成html(没有动态数据),那么解决方案将是预呈现html并作为静态内容。3)即使您不想呈现动态数据,而是为thymeleaf页面提供服务。。我认为您应该使用@Controller
返回thymeleaf html文件名作为响应。