欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

springboot配置templates直接访问的实现

时间:2022-02-19 09:40:04|栏目:JAVA代码|点击:

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","<h1>templates测试</h1>");
        model.addAttribute("users", Arrays.asList("lishao","liyuan"));
        return "/test";
    }
}

在controller中添加视图

在html中调用

<body>
<h3>test</h3>
<!--不转义-->
<div th:text="${msg}"></div>
<!--转义h1-->
<div th:utext="${msg}"></div>
<hr>
<h3 th:each="user : ${users}" th:text="${user}"></h3>

</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">

一个是转义一个是不转义

以下是运行的结果

在这里插入图片描述

在这里插入图片描述

上一篇:使用Java Api操作HDFS过程详解

栏    目:JAVA代码

下一篇:带你了解Java数据结构和算法之哈希表

本文标题:springboot配置templates直接访问的实现

本文地址:http://www.codeinn.net/misctech/193943.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有