springBoot项目中的static和templates文件夹

SpringBoot里面没有我们之前常规web开发的WebContent(WebApp),它只有src目录在src/main/resources下面有两个文件夹

static和templates springboot默认 static中放静态页面,而templates中放动态页面。但是webapp文件夹可以有,需要配置视图解析器同样可以访问。

1、static文件中(static文件默认是放静态资源的)

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

2、templates文件夹

放视图模板,就是springBoot的动态页面,需要引入thymeleaf组件

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

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

当然,也可以转发,放ip:port/index,打开static下的index目录中的index.html

@RequestMapping("index")
public String hello() {
    return "forward:/index/index.html";
}

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

编译之后static文件夹下的文件和templates文件夹下的文件其实会出现在同一个目录下,引入样式或者默认图片的时候注意

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

峰晨朴朴

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

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

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

打赏作者

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

抵扣说明:

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

余额充值