这篇文章给大家介绍如何进行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:方便后期查阅
@Controller
public class HelloController {
@RequestMapping("/test")
public String test(Model model){
model.addAttribute("msg","<h2>templates测试</h2>");
model.addAttribute("users", Arrays.asList("lishao","liyuan"));
return "/test";
}
}
在html中调用
<body>
<h4>test</h4>
<!--不转义-->
<div th:text="${msg}"></div>
<!--转义h2-->
<div th:utext="${msg}"></div>
<hr>
<h4 th:each="user : ${users}" th:text="${user}"></h4>
</body>
记得要导入templates的依赖
当你导入了templates依赖,
就会直接识别出来文件下的test,简单方便
<!--templates-->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
html中也要导入
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
一个是转义一个是不转义
以下是运行的结果
关于如何进行springboot配置templates直接访问的实现就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。