SpringBoot学习笔记(四)SpringBoot进行Web开发&Thymeleaf模板引擎

使用SpringBoot

  1. 创建SpringBoot应用,选中需要的模块
  2. SpringBoot已经进行了配置,我们在使用时只需要进行少量的配置即可
  3. 编写业务代码

SpringBoot 对静态资源的映射

“webjars/**”

所有 webjars/** 都到 classpath:META-INF/resource/webjars/ 找资源
https://www.webjars.org/
通过Maven引入前端框架。
示例如下:

<!--引入jQuery框架-->
<dependency>
   <groupId>org.webjars</groupId>
   <artifactId>jquery</artifactId>
   <version>3.5.1</version>
</dependency>
<script src="/webjars/jquery/3.5.1/jquery.js"></script>  

浏览器访问:http://127.0.0.1:8080/webjars/jquery/3.5.1/jquery.js

"/**"访问当前项目任何资源

"classpath:/META-INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/",
"/"  :当前项目的根路径

首页及图标设定

静态资源文件夹下的所有 index.html 被 /** 所映射
localhost:8080/ -> localhost:8080/index.html
所有的 **/favicon.ico 都是在静态资源文件下找
在这里插入图片描述

引入Thymeleaf 与使用

  1. 模板引擎
    JSP、Velocity、Freemarker、Thymeleaf(SpringBoot推荐)

Thymeleaf官方文档:https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html

示例如下:

  1. 引入依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
  1. 编写Controller
//这里使用了Thymeleaf的模板引擎,它自动的为我们拼接了前缀(template/)和后缀(.html)
@RequestMapping("/hi")
public String hi(Map map){
   map.put("hello","HELLO");
   return "hi";
}
  1. src\main\resources\templates\ 下创建一个 hi.html ,并为其导入thymeleaf命名空间
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>HI</title>
</head>
<body>
    <h1>This Is Hi Page</h1>
    <!-- 将div中的文本值设定为指定的内容 -->
    <div th:text="${hello}"></div>
</body>
<script src="/jquery.js" th:src="@{/webjars/jquery.3.5.1/jquery.js}"></script>
</html>
  1. 访问 http://127.0.0.1:8080/hi ,我们可以看到访问到了该页面的内容。

Thymeleaf语法

1. th属性:

在这里插入图片描述

2. 表达式

  1. ${…} 变量表达式,Variable Expressions
    //获取变量值,调用方法,使用内嵌基本对象
    在这里插入图片描述
  2. @{…} 链接表达式,Link URL Expressions
  3. #{…} 消息表达式,Message Expressions
  4. ~{…} 代码块表达式,Fragment Expressions
  5. *{…} 选择变量表达式,Selection Variable Expressions

引入资源

一般的静态资源包括html页面,建议放在templates目录下。这样可以让thymeleaf解析到。否则模板引擎无法解析。

  1. 目录结构
    在这里插入图片描述

问题:资源文件中存在两个静态的index.html

如果资源文件中存在两个静态的index.html,则在访问 127.0.0.1:8080/ 时,会访问static下的index.html。如何让它访问templates下的index.html呢?

  1. 解决方案一
@RequestMapping({"/","/index"})
public String index(){
    return "index";
}
  1. 解决方案二
//使用WebMvcConfigurationSupport来扩展SpringMVC的功能
//这里可以继承WebMvcConfigurerAdapter(已过期,建议使用WebMvcConfigurationSupport,这是对adapter的扩展)
// 也可以实现WebMvcConfigurer
@Configuration
public class MyMvcConfig extends WebMvcConfigurationSupport {

    @Override
    protected void addViewControllers(ViewControllerRegistry registry) {
        //浏览器发送/hx请求,请求到hi.html页面
        registry.addViewController("/hx").setViewName("hi");
        registry.addViewController("/").setViewName("index");
        registry.addViewController("/index").setViewName("index");
    }
}

关于使用addViewController无法映射不生效的问题

修改访问路径后前端会自动加上

  1. application.properties
server.servlet.context-path=/crud
  1. 访问 http://127.0.0.1:8080/crud/ 查看源码,可以看到thymeleaf已经为我们加上了前缀
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Huathy-雨落江南,浮生若梦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值