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

 

posted on 2018-11-30 14:04  legion  阅读(1640)  评论(1编辑  收藏  举报

导航