实训问题总结——Springboot项目静态网页static下可以访问,templates下不能访问

60 篇文章 0 订阅
20 篇文章 0 订阅

SpringBoot项目中templates目录的资源访问问题_springboot templates-CSDN博客

在SpringBoot项目中,一般都把静态资源放在 static目录 下,而页面放在 templates目录 下

SpringBoot默认静态资源映射文件夹是 /static/ 或 /public/ 或 /resources/ 或 /META-INF/resources/,并没有 templates目录,那么为什么我们平时使用起来却没有问题呢

原因是我们一般都整合了thymeleaf,Springboot和thymeleaf的整合中,对templates目录进行了处理,使我们可以通过Controller返回ModelAndView后,间接访问到templates目录下的资源,但是整合了thymeleaf后,也只能通过这种间接的方式访问,而不能直接访问。

想要直接访问访问thymeleaf目录下的静态资源,SpringBoot静态资源映射文件夹中添加templates目录。

以下举例:

情况一(假设有以下条件):

文件目录:

    resources:

        static:

            index.html

        templates:

            login.html

        application.yaml

application.yaml:    设置视图前缀、后缀

    spring:

      mvc:

        view:

          prefix: /

          suffix: .html

Controller中的方法:

    @RequestMapping("/index")

    public String test1(){

        return "index";

    }

    @RequestMapping("/login")

    public String test2(){

        return "login";

    }

 浏览器发起请求:

1、localhost:8080/index.html    访问index.html成功

2、localhost:8080/index         访问index.html成功

3、localhost:8080/login.html    访问login.html失败

4、localhost:8080/login         访问login.html失败

 情况二(假设有以下条件):

文件目录:

    resources:

        static:

            index.html

        templates:

            login.html

        application.yaml

application.yaml:    设置视图前缀、后缀

    spring:

      mvc:

        view:

          prefix: /

          suffix: .html

Controller中的方法:

    @RequestMapping("/index")

    public String test1(){

        return "index";

    }

    @RequestMapping("/login")

    public String test2(){

        return "login";

    }

pom.xml:    整合了thymeleaf

    <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-thymeleaf</artifactId>

    </dependency>

 浏览器发起请求:

1、localhost:8080/index.html    访问index.html成功

2、localhost:8080/index         访问index.html成功

3、localhost:8080/login.html    访问login.html失败

4、localhost:8080/login         访问login.html成功

 情况三(假设有以下条件):

文件目录:

    resources:

        static:

            index.html

        templates:

            login.html

        application.yaml

application.yaml:    设置视图前缀、后缀

    spring:

      mvc:

        view:

          prefix: /

          suffix: .html

      resources:    设置templates目录为映射目录

          static-locations: classpath:/static/,classpath:/templates/

Controller中的方法:

    @RequestMapping("/index")

    public String test1(){

        return "index";

    }

    @RequestMapping("/login")

    public String test2(){

        return "login";

    }

pom.xml:    整合了thymeleaf

    <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-thymeleaf</artifactId>

    </dependency>

 浏览器发起请求:

1、localhost:8080/index.html    访问index.html成功

2、localhost:8080/index         访问index.html成功

3、localhost:8080/login.html    访问login.html成功

4、localhost:8080/login         访问login.html失败

————————————————

版权声明:本文为CSDN博主「小廷没睡醒」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:SpringBoot项目中templates目录的资源访问问题_springboot templates-CSDN博客

  • 7
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

因上精进,果上随缘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值