14 springboot项目——首页跳转实现

        templates里的静态资源无法访问,需要写mvc的配置类或者改application.xml配置文件实现首页访问。这两个方式用其中一种即可,否则会冲突。

 14.1 首页跳转方式一

        创建配置类,在config包中创建一个mvc的配置类:

package jiang.com.springbootstudy.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MyMvcConfig implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index.html");
    }
}

14.2 首页跳转方式二

        修改application.yaml配置文件:server: servlert: context-path: /xxx,注意!这个方式最好不要用,不然无法使用mvc配置类进行跳转!

server:
  port: 8080
  servlet:
    context-path: /kuang

student:
  name: zhangsan
  age: 18
  arr: ["zhangsan","李四"]
spring:
  thymeleaf:
    cache: false

猜你喜欢

转载自blog.csdn.net/no996yes885/article/details/132082590
14