这篇文章主要介绍了springboot配置templates直接访问的实现方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
springboot配置templates直接访问
springboot下的templates目录的资源默认是受保护的,类似于javaweb项目的WEB-INF目录,但是给每个springboot的html页面都配置控制器跳转过于麻烦
配置公有访问方式如下
在配置文件加如下:
spring.resources.static-locations=classpath:/META-INF/resources/, classpath:/resources/, classpath:/static/, classpath:/templates/, classpath:/public/
附上spring 各种配置的官方url:方便后期查阅
springboot的templates用法
@Controller public class HelloController { @RequestMapping("/test") public String test(Model model){ model.addAttribute("msg","templates测试
"); model.addAttribute("users", Arrays.asList("lishao","liyuan")); return "/test"; } }
在controller中添加视图
在html中调用
test
记得要导入templates的依赖
当你导入了templates依赖,
就会直接识别出来文件下的test,简单方便
org.thymeleaf thymeleaf-spring5 org.springframework.boot spring-boot-starter-thymeleaf
html中也要导入
一个是转义一个是不转义
以下是运行的结果
以上为个人经验,希望能给大家一个参考,也希望大家多多支持0133技术站。
以上就是springboot配置templates直接访问的实现的详细内容,更多请关注0133技术站其它相关文章!