springboot的WEB开发笔记(静态资源,Thymeleaf,扩展自动配置类,Format)

1、静态资源

//第一种方法:
        if (!registry.hasMappingForPattern("/webjars/**")) {
            this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{"/webjars/**"}).addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"}).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
        }
//第二种方法:
        String staticPathPattern = this.mvcProperties.getStaticPathPattern();
        if (!registry.hasMappingForPattern(staticPathPattern)) {
            this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern}).addResourceLocations(WebMvcAutoConfiguration.getResourceLocations(this.resourceProperties.getStaticLocations())).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
        }

    }
}

 this.staticPathPattern = "/**";
 private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};
优先级高到低是:resources,static(默认)public
    
在yaml自定义spring.mvc.static-path-pattern会使得以上路径失效

2、访问首页

@Bean
public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext, FormattingConversionService mvcConversionService, ResourceUrlProvider mvcResourceUrlProvider) {
    WelcomePageHandlerMapping welcomePageHandlerMapping = new WelcomePageHandlerMapping(new TemplateAvailabilityProviders(applicationContext), applicationContext, this.getWelcomePage(), this.mvcProperties.getStaticPathPattern());
    welcomePageHandlerMapping.setInterceptors(this.getInterceptors(mvcConversionService, mvcResourceUrlProvider));
    welcomePageHandlerMapping.setCorsConfigurations(this.getCorsConfigurations());
    return welcomePageHandlerMapping;
}

private Optional<Resource> getWelcomePage() {
    String[] locations = WebMvcAutoConfiguration.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会被/**映射,所以访问http://localhost:8080/就可以访问到主页

3、图标

这个功能在新版springboot已经被移除,因为前端直接修改图标更快捷

在WebMvcAutoConfiguration自动配置类中可找到FaviconConfiguration方法即可使用

①首先在配置修改

#关闭默认图标
spring.mvc.favicon.enabled=false

②接着将favicon.ico图标文件放于静态文件中即可

注意:

版本更替快,有一些功能运用不了,可以去查看源码是否还存在该组件

4、Thymeleaf

ThymeleafProperies中所显示Thymeleaf所进行跳转的位置

public static final String DEFAULT_PREFIX = "classpath:/templates/";
public static final String DEFAULT_SUFFIX = ".html";

页面得先加:

<html lang="en" xmlns:th="http://www.thymeleaf.org" x>

语法运用:

对于后端传过来的model绑定的数据的调用

<div th:text="${msg}"></div>

5、扩展自动配置类

当你要自定义一些定制的功能,只需要写好组件把其交给springboot配置类,就会帮我们自动装配。

@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
    //实现了视图解析其接口的类,我们就可以把它看做视图解析器
    @Bean
    public  ViewResolver myViewResolver(){
        return  new MyViewResolver();
    }
    //定义一个自己的视图解析器
    public static class MyViewResolver implements ViewResolver{
        @Override
        public View resolveViewName(String s, Locale locale) throws Exception {
            return null;
        }
    }
}

第二种扩展(根据WebMvcConfigurer接口的方法进行重写,官方建议我们这样去扩展)

@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/cool").setViewName("index");
    }
}

小结:在springboot中有很多的XXXConfiguration帮助我们进行扩展配置,只要看见它,就得注意扩展了什么功能

6、Format

翻找源码可得配置为:spring.mvc.format.date=dd/MM/yyyy(该配置也与老师教学不同,因为版本问题的更替,所以查看源码进行配置的查看或者查看文档才能知道其运用的正确方法)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值