java

springBoot 项目中的静态资源static和模版文件te

2021-07-12  本文已影响0人  bianruifeng

springBoot 项目中的静态资源static和模版文件templates

springBoot 项目中的web资源在src/main/resources下的文件夹static和templates 中。

static 中放静态页面

templates 中放动态页面

在其他路径下也可以,需要配置视图解析器同样可以访问。

1、static文件默认是放静态资源的。

//这样写不能访问static中index文件夹下的index.html页面    
@RequestMapping("index")
    public String hello() {
        return "/index/index";
    }
//这样写才能访问到
@RequestMapping("index")
    public String hello() {
        return "/index/index.html";
    }

直接访问同样可以

http://localhost:8080/index.html

2、templates 文件夹 存放视图模版,模版需要引入thymeleaf组件。

templates 文件夹中的页面,需要引入thymeleaf 模板引擎后,经过控制器返回才能访问。

3、templates 文件中的页面引入static文件中的资源

编译之后static文件夹的内容和templates 文件夹的内容,会合并在统一目录下,在引入图片或者默认样式的时候注意。

4、springBoot项目中Controller层的页面重定向问题,需要引入thymeleaf组件

    @RequestMapping("index")
    public String hello() {
        return "/index/index.html";
    }
    
   //请求test会重定向到index     
    @RequestMapping("test")
    public String test() {
        return "redirect:/index";
    }      

总结:

静态页面的return默认是跳转到/static/目录下,当在pom.xml中引入了thymeleaf组件,动态跳转会覆盖默认的静态跳转,默认就会跳转到/templates/下,注意看两者return代码也有区别,动态没有html后缀。

上一篇 下一篇

猜你喜欢

热点阅读