栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

web开发之静态资源和欢迎页

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

web开发之静态资源和欢迎页

文章目录
        • 静态资源(static content)
          • 修改默认资源路径
          • 修改默认访问路径
        • 欢迎页面(welcome page)

新建Spring项目:demo1,如下所示。

添加了Lombok、Spring Configuration Processor和Spring Web这3个依赖。

自动生成的pom.xml如下所示。



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.6.2
         
    
    com.example
    demo2
    0.0.1-SNAPSHOT
    demo2
    Demo project for Spring Boot
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-configuration-processor
            true
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            org.projectlombok
                            lombok
                        
                    
                
            
        
    


将resources下的application.properties重命名为application.yml。

静态资源(static content)

SpringBoot默认类路径下的/static(或/public或resources或meta-INF/resources)目录下的文件为静态资源,默认/**可访问这些静态资源。
具体看例子。
在resources下新建以下4个目录,

  • static,并在static目录下放一张图片,a.jpg。
  • public,并在public目录下放一张图片,b.jpg。
  • resources,并在resources目录下放一张图片,c.jpg。
  • meta-INF/resources,并在meta-INF/resources目录下放一张图片,d.jpg。

启动应用,分别访问以下接口,

  • localhost:8080/a.jpg。
  • localhost:8080/b.jpg。
  • localhost:8080/c.jpg。
  • localhost:8080/d.jpg。

修改默认资源路径

SpringBoot也允许我们修改默认静态资源路径,在application.yml使用spring.web.resources.location即可实现,如下所示,

spring:
  web:
    resources:
      static-locations: [classpath:/hello/]

并将图片a.jpg放在resources下的hello目录下。
最后,重启应用,重新访问localhost:8080/a.jpg,依然可以访问成功。

按住Ctrl,并点击static-locations,进里面看看。

可以看到刚刚讲过的4个默认的静态资源路径。

private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/meta-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/"};
修改默认访问路径

SpringBoot也允许我们修改默认的静态资源访问路径,在application.yml使用spring.mvc.static-path-pattern即可实现,如下所示,

spring:
  mvc:
    static-path-pattern: /res/**

并将图片a.jpg放在resources下的static目录下。
最后,重启应用,访问localhost:8080/res/a.jpg,注意哈,不再是访问localhost:8080/a.jpg。

按住Ctrl,并点击static-path-pattern,进里面看看。

可以看到刚刚讲过的默认的静态资源访问路径,是/**。

private String staticPathPattern = "/**";

静态资源更多详细内容可以访问这里。

欢迎页面(welcome page)

关于欢迎页,SpringBoot官网有这么一段话,

Spring Boot supports both static and templated welcome pages. It first looks for an index.html file in the configured static content locations. If one is not found, it then looks for an index template. If either is found, it is automatically used as the welcome page of the application.

静态资源目录下的index.html默认是欢迎页面,试试看。
在resources下的static下创建index.html,内容如下,




    
    Title


hello

启动应用,并访问localhost:8080,如下,

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/683079.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号