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

web开发之静态资源和欢迎页,第1张

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,如下,

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5683638.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存