springboot配置视图解析器有两种方法;第一种方案在yml中配置,第二种方法就是在配置类中配置。

方案一:在属性文件application.properties中如下配置。

spring.mvc.view.prefix=/pages/
spring.mvc.view.suffix=.html

在application.yml中如下配置 。

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/ssm?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=utf8&useAffectedRows=true
    username: root
    password: 199000222
  mvc:
    view:
      prefix: /pages/
      suffix: .html

方案二:在配置类中如下。 

package com.stu.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
public class MvcConfig implements WebMvcConfigurer {

    /**
     * 配置视图解析器
     *
     * @return
     */
    @Bean
    public InternalResourceViewResolver configureInternalResourceViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/pages/");
        resolver.setSuffix(".html");
        return resolver;
    }
}

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐