springboot访问html静态等资源

本文详细介绍SpringBoot中静态资源的访问方式,包括如何在static目录下直接访问资源,以及如何通过配置访问static目录外的资源。同时,文章还讲解了如何使用Thymeleaf作为模板引擎,并通过controller注解返回视图。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、将html放在static文件目录下,可以直接访问,无需任何配置

在这里插入图片描述

2 、springboot如何访问static之外的静态资源

1、pom导入jar包

<!--导入html依赖-->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

2、application.yml

  thymeleaf:
    prefix: classpath:/templates/
    cache: true
    mode: HTML5
    encoding: UTF-8
    servlet:
      content-type: text/html --可以设置,也可以不设置,因为这是系统默认配置

3、编写controller

如果你想返回视图,也就是一个html页面,请使用controller注解
如果你想返回json数据,请使用restController注解

@Controller
public class TestController {
    @RequestMapping("/index")
    public String page(){
        System.out.println("hello shiro");
        return "shiro";
    }
}
前段输入http://localhost:8081/index  就会去访问/templates/shiro.html页面

在这里插入图片描述

4、springboot访问jsp页面

有空在写
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值