🎈博客主页:🌈我的主页🌈
🎈欢迎点赞 👍 收藏 🌟留言 📝 欢迎讨论!👏
🎈本文由 【泠青沼~】 原创,首发于 CSDN🚩🚩🚩
🎈由于博主是在学小白一枚,难免会有错误,有任何问题欢迎评论区留言指出,感激不尽!🌠个人主页
🌟 一、自定义首页
首先,创建两个html文件,一个为静态资源,另一个是动态资源(使用thymeleaf模板),进行映射配置,详细见博文
🌟🌟 1.1、模板
🌟🌟 1.2、映射配置
@Configuration
public class MyWebMvcConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/index").setViewName("index");
}
}
🌟🌟 1.3、结论
请求发送后,默认访问的是放在static静态目录的静态资源,如果将静态资源删除,那么将访问的就是template目录下的动态资源
🌟🌟 1.4、源码分析
进入WebMvcAutoConfiguration类中,找出getWelcomePage方法,这个方法就是用来请求资源的
private Resource getWelcomePage() {
for (String location : this.resourceProperties.getStaticLocations()) {
Resource indexHtml = getIndexHtml(location);
if (indexHtml != null) {
return indexHtml;
}
}
ServletContext servletContext = getServletContext();
if (servletContext != null) {
return getIndexHtml(new ServletContextResource(servletContext, SERVLET_LOCATION));
}
return null;
}
- this.resourceProperties.getStaticLocations()
拿到静态资源的位置"classpath:/META-INF/resources/",
“classpath:/resources/”, “classpath:/static/”, “classpath:/public/”,’’/’’ - Resource indexHtml = getIndexHtml(location)
拿到上述静态资源的位置,得到html文件
进入WelcomePageHandlerMapping类中,分析下列的方法
WelcomePageHandlerMapping(TemplateAvailabilityProviders templateAvailabilityProviders,
ApplicationContext applicationContext, Resource welcomePage, String staticPathPattern) {
if (welcomePage != null && "/**".equals(staticPathPattern)) {
logger.info("Adding welcome page: " + welcomePage);
setRootViewName("forward:index.html");
}
else if (welcomeTemplateExists(templateAvailabilityProviders, applicationContext)) {
logger.info("Adding welcome page template: index");
setRootViewName("index");
}
}
- if (welcomePage != null && “/”.equals(staticPathPattern)) {
logger.info("Adding welcome page: " + welcomePage);
setRootViewName(“forward:index.html”);
}
如果静态资源可以找到,那就返回找到的静态页面 - else if (welcomeTemplateExists(templateAvailabilityProviders, applicationContext)) {
logger.info(“Adding welcome page template: index”);
setRootViewName(“index”);
}
如果静态资源找不到,就找动态页面,动态页面存在就返回找到的动态页面
🌟 二、自定义浏览器角标
找到一个favicon制作的在线工具,制作icon,文件命名favicon.ico,加入到static的目录下,之后就会自动的加载到项目中去