【问题标题】:What kind of view technology used in spring boot by defaultspring boot默认使用什么样的视图技术
【发布时间】: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


    【解决方案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>
    

    【讨论】:

    • 这意味着,旧版本的spring boot提供了JSP作为默认视图。在 spring boot 2 中没有默认视图,我正确吗?
    • 为什么 spring boot 2 不提供任何视图?因为 spring boot starter 的主要目标是消除手动寻找 jars 的困难。
    【解决方案2】:

    开箱即用的 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>
    

    【讨论】:

      猜你喜欢
      • 2015-05-10
      • 2016-12-20
      • 1970-01-01
      • 2013-09-15
      • 2020-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多