springboot访问template下的html页面,配置详情

一、template下文件不允许直接访问

1、查资料得知:springboot项目默认是不允许直接访问template下的文件的,是受保护的。

     所以想访问template下的html页面,我们可以配置视图解析器。

2、如果想要用视图去展示,应该要设置好视图展示页面,比如说用一个模板语言来接收返回的数据(thymeleaf或者freemarker等), 也可以用jsp接收,但是SpringBoot官方是不推荐用jsp的,而是建议使用thymeleaf作为模板语言,这里我以thymeleaf为例。

二、配置步骤

1、pom.xml添加依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

2、application.yml中添加配置

spring:
  thymeleaf:
    prefix:
      classpath: /templates   # 访问template下的html文件需要配置模板,映射
    cache: false # 开发时关闭缓存,不然没法看到实时页面

3、template下添加一个index.html文件

4、后台action配置映射关系

这里有两种方法,经过尝试都可以访问 index.html 页面

三、结果展示

1、访问index1,返回到index.html页面了

2、访问index2,访问到html页面了

只不过,我这里没有返回数据,所以列表没有数据,但是返回到页面了

  • 23
    点赞
  • 60
    收藏
    觉得还不错? 一键收藏
  • 13
    评论
访问Spring Boot中的模板文件,可以按照以下步骤进行配置: 1. 在pom.xml文件中添加Thymeleaf的依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> ``` 2. 在application.yml或application.properties文件中配置Thymeleaf的相关属性: ```yaml spring: thymeleaf: prefix: classpath:/templates/ # 设置模板文件的路径 suffix: .html # 设置模板文件的后缀 ``` 3. 在resources/templates目录下创建模板文件,比如index.html。 4. 在Controller中定义处理请求的方法,并返回模板文件的名称: ```java @Controller public class MyController { @RequestMapping("/index") public String index() { return "index"; // 返回模板文件的名称,不需要指定后缀 } } ``` 这样,当访问"/index"路径时,Spring Boot会自动解析并渲染名为index.html的模板文件,并将其返回给客户端。 #### 引用[.reference_title] - *1* [springboot访问template下的html页面配置详情](https://blog.csdn.net/qq_35161328/article/details/93364230)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [SpringBoot项目中templates目录的资源访问问题](https://blog.csdn.net/m0_51512780/article/details/123227558)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值