软件编程
位置:首页>> 软件编程>> java编程>> springboot访问template下的html页面的实现配置

springboot访问template下的html页面的实现配置

作者:努力才能拥有一切  发布时间:2023-02-09 23:33:21 

标签: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文件

springboot访问template下的html页面的实现配置

4、后台action配置映射关系

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

springboot访问template下的html页面的实现配置

三、结果展示

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

springboot访问template下的html页面的实现配置

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

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

springboot访问template下的html页面的实现配置

来源:https://blog.csdn.net/qq_35161328/article/details/93364230

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com