网站首页 > 技术教程 正文
一.Spring Boot设置欢迎页面
1.默认欢迎页的源码
在Spring Boot中,默认的欢迎界面是index.html,那为什么这样呢?我们可以看看源码是怎么定义的。
public class WebMvcAutoConfiguration {
private Optional<Resource> getWelcomePage() {
String[] locations = getResourceLocations(this.resourceProperties.getStaticLocations());
return Arrays.stream(locations).map(this::getIndexHtml).filter(this::isReadable).findFirst();
}
private Resource getIndexHtml(String location) {
return this.resourceLoader.getResource(location + "index.html");
}
}
从源码中我们可以看到,欢迎页的静态资源文件默认就是index.html页面,并且只要该页面存放在resources目录下的默认路径中,就会被"/**"映射。
classpath:/META-INF/resources/
classpath:/resources/
classpath:/static/
classpath:/public/
/:当前项目的根路径
也就是只要index.html页面在以上几个目录内,就会自动被Spring Boot探测到。
2.实验验证
2.1 创建web项目(略)
目录结构如下,在该项目中,我们在resources目录下,创建4个子文件夹,具体参考上一章节。然后在每个子文件夹中,都存放一个index.html文件,但是文件内容略有不同,每个文件都有自己的编号。
每个index.html文件内容的编号不同,以此类推!
2.2 启动项目测试
我们启动web项目,输入地址http://localhost:8080会发现,默认加载的是META-INF/resources目录下的index.html文件,为什么呢?这与静态资源文件夹的优先级有关系哦,我们上一章节已经讲过了
二.自定义欢迎页面
但在实际开发中,我们有时候就希望先访问登录界面,然后登录成功后再跳到主页面,那此时如何将登录页面作为欢迎页面呢?
这个可以有两种实现方式。
- 视图控制器配置方式;
- Controller直接实现方式;
1.视图控制器配置方式
1.1 创建配置类
我们可以在上面的web项目中,创建一个WebMvcConfigurerAdapter类。
package com.yyg.boot.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;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
/**
* @Description Description
* @Author 一一哥Sun
* @Date Created in 2020/3/21
*/
@Configuration
public class DefaultViewConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
//这里的"/"是访问路径,"forward:home.html"是请求转发到的页面名称
registry.addViewController("/").setViewName("forward:home.html");
//设置优先级
registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
super.addViewControllers(registry);
}
}
1.2 创建home.html页面
我们在static目录下创建一个home.html页面。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Home</title>
</head>
<body>
<h1>一一哥的Home页面...</h1>
</body>
</html>
1.3 运行测试
接着我们运行程序,输入地址:http://localhost:8080就可以看到如下欢迎界面。
2. Controller直接实现方式
我们在上一个例子的基础之上,创建一个Controller类。
注意:
把上一个案例中DefaultViewConfig配置类的@Configure注解去掉,避免影响到本次实验。
2.1 项目结构:
2.2 创建Controller
package com.yyg.boot.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @Description Description
* @Author 一一哥Sun
* @Date Created in 2020/3/21
*/
@Controller
public class WelcomeController {
@RequestMapping("/")
public String view() {
return "forward:home.html";
}
}
2.3 运行测试
接着我们运行程序,输入地址:http://localhost:8080就可以看到如下欢迎界面。
三.结合Thymeleaf模板实现欢迎页面
我们可以结合Thymeleaf模板,来实现欢迎页面。
1.添加依赖包
在该web项目的pom.xml文件中添加依赖包。
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
2.配置模板路径
创建application.properties文件,添加如下配置,其实默认也是这个配置。
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
3.创建login.html页面
创建login.html存放到/templates/目录下。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Login</title>
</head>
<body>
<h1>一一哥的登录页面...</h1>
</body>
</html>
4.创建Controller接口
package com.yyg.boot.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @Description Description
* @Author 一一哥Sun
* @Date Created in 2020/3/21
*/
@Controller
public class WelcomeController {
// @RequestMapping("/")
// public String view() {
// return "forward:home.html";
// }
@RequestMapping("/")
public String login() {
return "login";
}
}
5.运行测试
输入地址,http://localhost:8080即可看到欢迎界面。
四.设置网站图标Favicon
1.Favicon简介
很多时候,企业网站一般都会有一个对应的网站图标(Favicon),在浏览器访问网站时,对应的浏览器标签上会出现对应的图标。例如csdn网站上的小图标。
2.Favicon源码分析
我们可以看看Spring中关于Favicon的源码。
@Configuration
@ConditionalOnProperty(value = {"spring.mvc.favicon.enabled"},matchIfMissing= true)
public static class FaviconConfiguration implements ResourceLoaderAware {
private final ResourceProperties resourceProperties;
private ResourceLoader resourceLoader;
public FaviconConfiguration(ResourceProperties resourceProperties) {
this.resourceProperties = resourceProperties;
}
public void
setResourceLoader(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
}
@Bean
public SimpleUrlHandlerMapping faviconHandlerMapping() {
SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
mapping.setOrder(-2147483647);
mapping.setUrlMap(Collections.singletonMap("**/favicon.ico", this.faviconRequestHandler()));
return mapping;
}
@Bean
public ResourceHttpRequestHandler faviconRequestHandler() {
ResourceHttpRequestHandler
requestHandler = new ResourceHttpRequestHandler();
requestHandler.setLocations(this.resolveFaviconLocations());
return requestHandler;
}
private List<Resource> resolveFaviconLocations() {
String[] staticLocations = WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter.getResourceLocations(this.resourceProperties.getStaticLocations());
List<Resource> locations = new ArrayList(staticLocations.length + 1);
Stream var10000 = Arrays.stream(staticLocations);
ResourceLoader var10001 = this.resourceLoader;
var10001.getClass();
var10000.map(var10001::getResource).forEach(locations::add);
locations.add(new ClassPathResource("/"));
return
Collections.unmodifiableList(locations);
}
}
3.SpringBoot 1.x与2.x版本区别
在SpringBoot 1.x版本中对Favicon进行了默认支持,并且可以通过如下配置进行关闭操作:
spring.mvc.favicon.enabled=false ## 关闭
默认的Favicon图标效果:
但到了SpringBoot2.x版本后,在Spring Boot项目的issues中提出,如果用户不进行自定义的Favicon的设置,而Spring Boot项目会提供默认的图标,而如果提供默认的Favicon图标,则可能会导致泄露网站的开发框架这样的信息。
因此,在Spring Boot2.2.x中,将默认的favicon.ico移除,同时也不再提供上述application.properties中的属性配置。更多详细信息可查看对应的issues:https://github.com/spring-pr
4.SpringBoot2.x项目中添加Favicon
在2.x以前的版本,直接将你需要的favicon.ico文件存放在static下面就可以。
但到了2.2.X以后的版本,去掉了默认的自动配置,需要我们手动在每一个页面添加自己网站的Favicon图标。
我们可以在static目录下创建一个images目录,里面存放自己的Favicon.ico图标。
4.1 html页面中添加
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Login</title>
<link rel="icon" href="images/Favicon.ico" type="image/x-icon"/>
</head>
<body>
<h1>一一哥的登录页面...</h1>
</body>
</html>
4.2 Thymeleaf页面中添加
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>Hello Favicon</title>
<link rel="icon" th:href="@{/favicon.ico}" type="image/x-icon"/>
</head>
<body>
<h1>Hello 一一哥!</h1>
</body>
</html>
5.重启项目测试
我们重新访问页面,可以看到Favicon图标已经换成了我自己的图标。
- 上一篇: 这 10 个 Web 开发工具我每天都在用
- 下一篇: 老司机都爱这样设置浏览器,体验爽 10 倍
猜你喜欢
- 2024-09-25 工具推荐:dismap 快速资产发现和识别工具
- 2024-09-25 谷歌优化Chrome全局媒体控件 调整专辑封面尺寸
- 2024-09-25 分享几个优质的油猴脚本 油猴脚本推荐排行2020
- 2024-09-25 电商平台被入侵,黑客通过图片盗取电商平台用户隐私信息
- 2024-09-25 网站地址前的小图标怎么添加 网站后面加地址后缀
- 2024-09-25 秒开WebView?Android性能优化全攻略
- 2024-09-25 【Python程序开发系列】介绍一款轻量级高自由度web框架-NiceGUI
- 2024-09-25 Zotero文献管理 | 添加文献检索引擎(附下载)
- 2024-09-25 Go 每日一库之 negroni 每日一占
- 2024-09-25 这5个好玩又实用的在线工具,你还不收藏吗?
你 发表评论:
欢迎- 05-1613步震撼淘宝大促闪光裂纹破墙立体字PS制作教程
- 05-16AI教程 | 绘制扁平的萌萌哒图标
- 05-160基础学平面设计所需了解的基础常识汇总
- 05-16自学平面设计需要多长时间?十六年职业设计总监告诉你
- 05-16平面设计都要学习哪些内容?
- 05-16李涛PS教程 高手之路PS教程 合成教程 —制作一个小星球
- 05-16Illustrator实例教程:制作炫酷的漩涡效果
- 05-16Illustrator实例教程:利用混合工具制作一朵炫酷的花
- 最近发表
- 标签列表
-
- sd分区 (65)
- raid5数据恢复 (81)
- 地址转换 (73)
- 手机存储卡根目录 (55)
- tcp端口 (74)
- project server (59)
- 双击ctrl (55)
- 鼠标 单击变双击 (67)
- debugview (59)
- 字符动画 (65)
- flushdns (57)
- ps复制快捷键 (57)
- 清除系统垃圾代码 (58)
- web服务器的架设 (67)
- 16进制转换 (69)
- xclient (55)
- ps源文件 (67)
- filezilla server (59)
- 句柄无效 (56)
- word页眉页脚设置 (59)
- ansys实例 (56)
- 6 1 3固件 (59)
- sqlserver2000挂起 (59)
- vm虚拟主机 (55)
- config (61)
本文暂时没有评论,来添加一个吧(●'◡'●)