SpringBoot访问静态资源(图片、html)以及整合thymeleaf 注意点

static和templates 是springboot默认创建的文件夹,static中放静态页面,而templates中放动态页面

存放在static目录下,浏览器可以直接访问 http://localhost:8080/index.html,图片资源以及css,js可以放在此目录下,而templates 不能访问

若整合thymeleaf,需要引入jar  

implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'

编写controller进行跳转 

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.HashMap;

@Controller
public class IndexController {


    /*
        本地访问内容地址 :http://localhost:8080/hello
     */
    @RequestMapping("/hello")
    public String helloHtml(HashMap<String, Object> map) {
        map.put("hello", "欢迎进入HTML页面");
        return "/index";
    }

/*

    当引入了thymeleaf组件,动态跳转会覆盖默认的静态跳转
    想跳转到/static/index.html,可以使用重定向return "redirect:/index.html"

 */
    @RequestMapping("/hello2")
    public String hello2Html() {

        return "redirect:/index.html";
    }


}

动态的html代码

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8"/>
    <title>templates中放动态页面</title>
</head>
<body>
<h1>templates中放动态页面</h1>

<p th:text="${hello}"></p>


</body>
</html>

 

静态html代码

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8"/>
    <title>static中放静态页面</title>
</head>
<body>

    <h1> static中放静态页面 </h1>

</body>
</html>

 

效果图如下

                  

 

 

若在static下创建文件夹如css,js,images,必须在application.yml文件中进行声明,即使是默认的配置,否者无法访问

mvc:
  static-path-pattern: /**

 

 

 

转载于:https://my.oschina.net/shanesen/blog/3015059

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值