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文件夹下的文件其实会出现在同一个目录下,引入样式或者默认图片的时候注意
到此这篇关于springBoot项目中的static和templates文件夹的使用的文章就介绍到这了,更多相关springBoot static和templates文件夹内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
springboot中使用FastJson解决long类型在js中失去精度的问题
这篇文章主要介绍了springboot中使用FastJson解决long类型在js中失去精度的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2022-06-06
SpringCloud及Nacos服务注册IP选择问题解决方法
这篇文章主要介绍了SpringCloud及Nacos服务注册IP选择问题,为什么注册的IP和真实IP不符合呢,原因是Nacos客户端在注册服务时会从机器网卡中选择一个IP来注册,所以,当注册了的是非真实IP后,另一台机器调用时是不可能调通的,知道问题原因就是解决方法,一起看看吧2024-01-01
聊聊springmvc中controller的方法的参数注解方式
本篇文章主要介绍了聊聊springmvc中controller的方法的参数注解方式,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2017-10-10


最新评论