使用Spring Boot在嵌入式Tomcat服务器上部署资源
我有一个项目,其中来自多个来源的数据正在被处理成一些数据结构。在程序构建完这些结构之后,我希望它设置一个服务器,使用户能够手动微调这些结构。我决定使用SpringBoot在嵌入式Tomcat服务器上安装SpringMVC正是我所需要的 我想使用Thymeleaf作为查看技术,因此我做到了使用Spring Boot在嵌入式Tomcat服务器上部署资源,spring,tomcat,spring-mvc,spring-boot,embedded-tomcat-7,Spring,Tomcat,Spring Mvc,Spring Boot,Embedded Tomcat 7,我有一个项目,其中来自多个来源的数据正在被处理成一些数据结构。在程序构建完这些结构之后,我希望它设置一个服务器,使用户能够手动微调这些结构。我决定使用SpringBoot在嵌入式Tomcat服务器上安装SpringMVC正是我所需要的 我想使用Thymeleaf作为查看技术,因此我做到了 @Configuration @ComponentScan @EnableAutoConfiguration public class Main { public static void main(S
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Main {
public static void main(String... args) throws Exception {
// Lots of initialization ...
SpringApplication.run(Main.class, args);
}
@Bean
public ServletContextTemplateResolver templateResolver() {
ServletContextTemplateResolver resolver = new ServletContextTemplateResolver();
resolver.setPrefix("/resources/views/");
resolver.setSuffix(".html");
resolver.setTemplateMode("HTML5");
resolver.setCacheable(false);
return resolver;
}
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine engine = new SpringTemplateEngine();
engine.setTemplateResolver(templateResolver());
return engine;
}
@Bean
public ViewResolver viewResolver() {
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(templateEngine());
viewResolver.setOrder(1);
viewResolver.setViewNames(new String[]{"*"});
viewResolver.setCache(false);
return viewResolver;
}
}
及
但是,即使在/resources/views/greeting.html
上有一个视图文件,服务器对URL的回复仍然是http://localhost:8080/greeting
org.thymeleaf.exceptions.TemplateInputException: Error resolving template "greeting", template might not exist or might not be accessible by any of the configured Template Resolvers
在调试器中单步执行代码后,似乎在某个点上,ServletContext
(应该以流的形式返回视图文件)会出现在一个临时文件夹中,如
C:\Users\Michael\AppData\Local\Temp\tomcat-docbase.971027024999448548.8080
它是空的
现在我明白了,我也需要
greeting.html
放入/resources/templates/greeting.html
并将资源
添加到构建路径后,我得到了错误
javax.servlet.ServletException: Circular view path [greeting]: would dispatch back to the current handler URL [/word/greeting] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)
换句话说,Thymeleaf似乎没有正确配置。经过一番修改,我最终将pom.xml
中的spring boot starter parent
版本从0.5.0.BUILD-SNAPSHOT
更改为0.5.0.M6
:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<!--<version>0.5.0.BUILD-SNAPSHOT</version>-->
<version>0.5.0.M6</version>
</parent>
org.springframework.boot
spring启动程序父级
0.5.0.M6
以及从Thymeleaf依赖项中删除版本标记
<dependencies>
<!-- ... -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring3</artifactId>
<!--<version>${thymeleaf-spring3-version}</version>-->
</dependency>
</dependencies>
org.thymeleaf
百里香
在这之后,它起了作用
有人能解释一下为什么我需要更改
spring boot starter parent
的版本,以便能够从thymeleaf-spring3
中删除版本标记,以及为什么这是必要的吗?servlet上下文根不是嵌入式服务器中模板的最佳位置。有一种方法可以做到这一点,但如果我是你的话,我会顺其自然,使用类路径。如果允许Spring Boot配置模板解析器(也推荐),那么默认情况下它将在classpath:/templates
中查找。有几个示例在启动代码库中使用了thymeleaf,因此如果您有不同的需求,应该可以很容易地修改其中的一个。Spring Boot对Spring 3不起作用(很多),因此当thymeleaf发布其Spring 4软件包时,启动会切换到使用这些。工件id是不同的,这就是为什么它不适用于旧的。这个变化肯定是在M6之后发生的,所以如果您想要最新和最好的(可能包括M7),您需要thymeleaf spring4依赖项。
<dependencies>
<!-- ... -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring3</artifactId>
<!--<version>${thymeleaf-spring3-version}</version>-->
</dependency>
</dependencies>