it编程 > 编程语言 > Java

SpringBoot设置欢迎页的三种方式详解

9人参与 2026-01-31 Java

方式一:springboot对静态资源的自动映射(最基础)

springboot框架将欢迎页做了自动配置,不需要任何配置,只需在静态资源目录下添加index.jsp欢迎页即可。

1. 导入jquery‐webjar依赖

<!--引入jquery‐webjar 在访问的时候只需要写webjars下面资源的名称即可-->
<dependency>
    <groupid>org.webjars</groupid>
    <artifactid>jquery</artifactid>
    <version>3.3.1</version>
</dependency>

2. 在 src/main/resources/static/ 下新建一个 index.html 

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>title</title>
</head>
<body>
    <h2>springboot对静态资源的自动映射</h2>
    <p>springboot自动配置,在静态资源下查找index.html文件</p>
</body>
</html>

3. 访问当前项目的任何资源,都去(静态资源的文件夹)找映射

优先级:从高到低

# 任选其一
"classpath:/meta‐inf/resources/index.html",
"classpath:/resources/index.html",
"classpath:/static/index.html",
"classpath:/public/index.html"
"/":当前项目的根路径

4. 适用场景:

方式二:springboot-controller映射(最常用)

最常用的一种获取动态页面的方式,页面通常放在classpath:/templates/目录下(模板引擎默认目录)。

1. 在 src/main/resources/templates/ 下新建一个 index.html 

<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>title</title>
</head>
<body>
    <h2 th:text="${message}"></h2>
    <h2>且使用thymeleaf 模板引擎</h2>
</body>
</html>

2. 在controller层建一个java文件

package com.qcby.springbootwelcome.controller;

import org.springframework.stereotype.controller;
import org.springframework.ui.model;
import org.springframework.web.bind.annotation.requestmapping;

@controller
public class welcomecontroller {
    @requestmapping("/")
    public string welcomehello(model model){
        model.addattribute("message","springboot-controller映射");
        return "index";
    }
}

3. 访问路径:classpath:/templates/index.html

4. 适用场景:

方式三:webmvcconfigurer 配置类

1. 在 src/main/resources/templates/ 下新建一个 login.html 

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>title</title>
</head>
<body>
    <h2>登录页面</h2>
    <h3>适用 webmvcconfigurer 配置类</h3>
</body>
</html>

2. 创建webconfig配置类

package com.qcby.springbootwelcome.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 webconfig implements webmvcconfigurer{
    @override
    public void addviewcontrollers(viewcontrollerregistry registry) {
        // 直接映射到静态页面
//        registry.addviewcontroller("/").setviewname("login.html");

        // 或者映射到模板引擎页面
         registry.addviewcontroller("/").setviewname("login");
    }
}

3. 访问路径:classpath:/templates/login.html

4. 适用场景:

总结

这三种方式的优先级:springboot-controller映射 > webmvcconfigurer 配置类 > springboot对静态资源的自动映射

方法适用场景优点缺点
静态资源默认简单静态页面无需配置功能单一
webmvcconfigurer需要url映射灵活需要代码配置
controller方式动态内容功能强大代码量多
模板引擎动态页面前后端结合需要学习模板语法

到此这篇关于springboot设置欢迎页的三种方式详解的文章就介绍到这了,更多相关springboot设置欢迎页内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

您想发表意见!!点此发布评论

推荐阅读

JAVA中Spring Boot的AOP切面编程是什么,如何使用?(实例代码)

01-31

MyBatis中批量插入的三个关键优化技巧与避坑指南

01-31

java的多重注解(重复注解)详解

01-31

Spring Boot全局异常处理机制中DispatcherServlet的处理流程和作用

01-31

Java多重数组使用及说明

01-31

Spring Boot异常处理try-catch应该怎么使用?

01-31

猜你喜欢

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论