Spring Boot默认使用哪种视图技术

时间:2019-12-05 05:18:41

标签: spring spring-boot

当我添加“ Spring Boot Web Starter”时,默认情况下在Spring Boot中使用哪种视图技术。 如果要使用JSP,则需要为百里香叶模板包括'tomcat-embed-jasper'或'Spring Boot Thymeleaf Starter'。所以我想知道'Spring Boot Web Starter'的默认视图技术

2 个答案:

答案 0 :(得分:1)

默认情况下没有视图,您需要配置并添加它们的依赖关系。如果您使用的是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>

答案 1 :(得分:0)

现成的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

对于Thymeleaf

Spring Boot将为pom.xml中具有以下依赖性的Thymeleaf提供自动配置

请记下使用的版本。另外,您可能需要提供如上所述的视图属性

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
    <version>1.5.6.RELEASE</version>
</dependency>