• IDEA Springboot 视图解析二器和静态资源的一些问题


    1.首先认识一下resources目录下三个文件夹的作用。

    public

    相当于web-inf文件夹外的文件,外部通过主机加文件名的方式可以直接访问。

    比如访问127.0.0.1:8080/mypublic.html

    访问public目录下任何格式文件都可以,但是子目录中的不行。

    static

    目录名称为css,html,js下面后缀名为html,css,js的文件可以从浏览器直接访问。

    比如访问http://127.0.0.1:8080/css/easyloader.js

    http://127.0.0.1:8080/html/no.html  ,但是这些目录中的子目录文件不可外部直接访问。

    static目录下的index.html文件可以直接访问。访问主机127.0.0.1会直接访问127.0.0.1/index.html

    template

    Thymeleaf转发会使用该目录下的html文件

    通过Thymeleaf

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-thymeleaf</artifactId>
            </dependency>

    Controller中

    @Controller
    public class HelloController {
        @RequestMapping(value = "/hello", method = RequestMethod.GET)
        public String say() {
            return "hello";
        }
    }

    这样返回“hello”就是forward内部templates中的hello.html。

     转发static下的html文件

    如图,static目录下有goodie.html,unknown子目录下有leigon.html文件,HTML子目录下的go目录有no.html文件

    现在通过controller来转发这些文件。

    首先在application.yml中配置视图解析器,删除Thymeleaf库,重新build。

    spring:
      mvc:
        view:
          prefix: /
          suffix: .html

     

    会转发到对应的html

        @GetMapping("/good")
        public String goo() {
            return "goodie";
        }
    
        @GetMapping("/legion")
        public String legion() {
            return "/unknown/legion";
        }
    
        @GetMapping("/no")
        public String no() {
            return "/html/go/no";
        }
    View Code
  • 相关阅读:
    HTTP 状态码大全
    Redis Cluster数据分片机制
    redis 哨兵集群原理及部署
    python 连接 redis cluster 集群
    python连接redis哨兵集群
    Ubuntu设置终端操作行为的回收站
    实用Golang库
    实用的Python库
    Python 生成 JWT(json web token) 及 解析方式
    django 进行语言的国际化及在后台进行中英文切换
  • 原文地址:https://www.cnblogs.com/legion/p/10043770.html
Copyright © 2020-2023  润新知