spring boot 设置默认主页

一 概念

主页:访问网站域名跳转的第一个页面

二 原理
三 使用
环境

idea:2019
springboot:2.0.1.RELEASE
jdk:1.8

1)默认的方式

在resources目录下面创建一个static文件夹,
在static文件夹下面创建一个index.html文件,
不需要任何其他的配置即可完成主页的设置

2)指定某一个页面作为主页

编写一个controller类,该类中有一个方法的匹配路径为/

package com.myworld.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 主页控制器
 */
@Controller
@RequestMapping(value = "/")
public class IndexController {
    @RequestMapping(value = "/")
    public String index(){
        System.out.println("/index");
        return "/html/pages/samples/login";
    }
}

或者
添加一个继承自WebMvcConfigurerAdapter的配置类即可

package com.myworld.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

//或者实现WebMvcConfigurer 接口
//public class DefaultView implements WebMvcConfigurer {
//WebMvcConfigurerAdapter已经过时
@Configuration
public class DefaultView extends WebMvcConfigurationSupport {

    /**
     * 添加主页方法
     *
     * @param registry 主页注册器
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        System.out.println("设置了主页");
        //设置主页
        registry.addViewController("/").setViewName("/html/pages/samples/login");
        //设置优先级
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
        //将主页注册器添加到视图控制器中
        super.addViewControllers(registry);
    }
}

两种设置的视图都是一样的

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值