【发布时间】:2020-03-30 00:04:31
【问题描述】:
当我添加'Spring Boot Web Starter'时,spring boot默认使用什么样的视图技术。 如果我想使用 JSP,我需要为 thymeleaf 模板包含“tomcat-embed-jasper”或“Spring Boot Thymeleaf Starter”。所以想知道'Spring Boot Web Starter'的默认视图技术
【问题讨论】:
标签: spring spring-boot
当我添加'Spring Boot Web Starter'时,spring boot默认使用什么样的视图技术。 如果我想使用 JSP,我需要为 thymeleaf 模板包含“tomcat-embed-jasper”或“Spring Boot Thymeleaf Starter”。所以想知道'Spring Boot Web Starter'的默认视图技术
【问题讨论】:
标签: spring spring-boot
默认情况下没有视图您需要配置和添加它们的依赖项。如果您使用的是 Spring Boot 旧版本,那么您可以参考上面的答案,但如果您使用的是 Spring Boot 2,则为 thymeleaf 添加更多依赖项-
<dependency>
<groupId>nz.net.ultraq.thymeleaf</groupId>
<artifactId>thymeleaf-layout-dialect</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
【讨论】:
开箱即用的 Spring 支持JSP。
可以这样配置
@EnableWebMvc
@Configuration
public class ApplicationConfiguration implements WebMvcConfigurer {
@Bean
public ViewResolver jspViewResolver() {
InternalResourceViewResolver bean = new InternalResourceViewResolver();
bean.setPrefix("/WEB-INF/views/");
bean.setSuffix(".jsp");
return bean;
}
}
或在属性文件中
spring.mvc.view.prefix: /WEB-INF/views/
spring.mvc.view.suffix: .jsp
百里香
Spring Boot 将为 Thymeleaf 提供自动配置,在 pom.xml 中具有以下依赖项
请记下使用的版本。此外,您可能需要提供上述视图属性
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>1.5.6.RELEASE</version>
</dependency>
【讨论】: