进阶SpringBoot之首页和图标定制

idea 快捷键:

ctrl + shift + "+" 使缩起来的代码展开

ctrl + shitf + "-" 使代码缩起

WebMvcAutoConfiguration.class:可以看到需要有一个 index.html 文件映射到首页

java 复制代码
        private Resource getIndexHtmlResource(Resource location) {
            try {
                Resource resource = location.createRelative("index.html");
                if (resource.exists() && resource.getURL() != null) {
                    return resource;
                }
            } catch (Exception var3) {
            }

            return null;
        }

resources 目录下 任意一个(图片上有的)都能存放 index.html,随便写个首页两字

(放在 templates 目录下的所有页面,只能通过 controller 来跳转)

如果放在 templates 包下,需要有 thymeleaf 插件

java 复制代码
package com.demo.web.controller;

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

@Controller
public class IndexController {
    //在templates目录下的所有页面,只能通过controller来跳转
    @RequestMapping ("/index")
    public String index(){
        return "index";
    }
}

同理,把图片命名为 favicon.ico,放到 resources 目录任意一个包下

启动查看效果:

图标成功显示!

相关推荐
zs宝来了几秒前
Spring Boot 内嵌 Tomcat 原理:Tomcat ServletWebServerFactory 源码解析
spring boot·tomcat·内嵌容器·webserverfactory
zzb15806 分钟前
Agent案例-智能文档问答助手
java·人工智能·笔记·python
dd向上13 分钟前
【计算机毕设/课设】在职全栈开发工程师接单:Java(SpringBoot+Vue)/小程序/C++(Qt/MFC) 定制与辅导
java·spring boot·课程设计
ckm紫韵15 分钟前
Maven搭建私服Nexus教程
java·maven·nexus
故以往之不谏16 分钟前
JAVA--类和对象4.1--构造方法基础
java·开发语言·javascript
014-code18 分钟前
Java Optional 那些被忽略的用法
java·数据库·javase
A 小码农20 分钟前
亲测AI智能小助手-IDEA中使用腾讯混元大模型
java·人工智能·intellij-idea
eggwyw20 分钟前
redis 使用
java
ERBU DISH22 分钟前
ChatGLM2-6B模型推理流程和模型架构详解
java
BullSmall27 分钟前
IntelliJ IDEA 安装与环境配置指南(2026 最新)
java·ide·intellij-idea