(八)Spring Boot Web开发——静态资源访问和Thymeleaf 动态渲染

1、静态资源访问

Spring Boot 默认会挨个从META/resources > resources > static > public 里面找是否存在相应的资源,如果有则直接返回
在这里插入图片描述


1.1 默认配置

Spring Boot默认提供静态资源目录位置需置于classpath下,目录名需符合如下规则:
/static
/public
/resources
/META-INF/resources

举例:在src/main/resources/目录下创建public,在该位置创建public.html文件。启动程序后,尝试访问http://127.0.0.1:8888/study/public.html。
在这里插入图片描述


1.2 修改静态资源文件配置

增加一个image目录
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/image

直接访问增加的image目录的的图片:
在这里插入图片描述



2、请求转发和重定向

重定向redirect

@RequestMapping(path="/view/index_redirect")
public String getPage_redirect() {
//return “redirect:/page/view/index_forword”;
return “redirect:/public.html”;
}

转发forward

@RequestMapping(path="/view/index_forword")
public String getPage_forword() {
//return “forward:/page/view/index_redirect”;
return “forward:/public.html”;
}

3、渲染Web页面

3.1 整合thymeleaf模板引擎

3.1.1 Thymeleaf 常用的表达式、标签和函数

1.常用表达式

  • ${...}变量表达式。
  • * { .. . } 选择表达式。
  • #{...}消息文字表达式。
  • @ {} 链接url 表达式。
  • #maps 工具对象表达式。

2.常用标签

  • th:action 定义后台控制器路径。
  • th:each 1,盾环语-句。
  • th:field 表单字段绑定。
  • th:href 定义超链接。
  • th:id div 标签中的ID 声明,类似HTML 标签中的归属性。
  • th:if 条件判断语句。
  • th:include 布局标签,替换内容到引入文件。
  • th :企agment 布局标签,定义一个代码片段,方便其他地方引用。
  • th:object 替换对象。
  • th:src 图片类地址引入。
  • th:text 显示文本。
  • th:value 属性赋值。

3.常用函数

  • #dates 日期函数。
  • #lists 列表函数。
  • #arrays 数组函数。
  • #strings 字符串函数。
  • #numbers 幸生字函捷生。
  • #ca lendars 日历函数。
  • #objects 对象函数。
  • #bools 逻辑函数。

3.1.2 整合Thymeleaf的简单demo

  • 第一步:添加依赖
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

  • 第二步:添加配置(可不配置,全都按照默认配置)
###ThymeLeaf配置
#模板的模式,支持 HTML, XML TEXT JAVASCRIPT
spring.thymeleaf.mode=HTML5
#编码 可不用配置
spring.thymeleaf.encoding=UTF-8
#开发配置为false,避免修改模板还要重启服务器
spring.thymeleaf.cache=false
#配置模板路径,默认是templates
spring.thymeleaf.prefix=classpath:/templates/
#配置模板文件的后缀
spring.thymeleaf.suffix=.html

  • 第三步:编写模板和Controller接口
@RequestMapping(path="/view/index3")
public ModelAndView getPage_3() {
    ModelAndView model = new ModelAndView();
    model.setViewName("page/user");
    model.addObject("name", "小明");
    return model;
}

模本文件的存放路径一定要和spring.thymeleaf.prefix一致,默认是classpath:/templates/

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8" />
<title></title>
</head>
<body>
	page/page目录下: username-><span th:text="${name}"></span>
</body> 
</html>

在这里插入图片描述


  • 第四步:测试访问
    可以看到模板中的<span th:text="${name}"></span>被替换为了小明

在这里插入图片描述

4、项目git地址

spring-boot2-http

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要搭建Spring Boot使用Thymeleaf视图模板进行Web开发的环境,可以按照以下步骤进行: 1. 创建Spring Boot项目 可以使用Spring Initializr或者IDE(如IntelliJ IDEA)创建一个Spring Boot项目,选择WebThymeleaf作为依赖项。 2. 添加Thymeleaf配置 在应用程序的配置文件中(如application.properties或application.yml),添加以下配置: ``` spring.thymeleaf.cache=false spring.thymeleaf.enabled=true spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html ``` 这里的配置指定了Thymeleaf的模板文件所在的路径和后缀名。 3. 创建Thymeleaf模板 在src/main/resources/templates目录下创建一个HTML文件,并添加Thymeleaf的标签和表达式。例如: ``` <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>Thymeleaf Example</title> </head> <body> <h1 th:text="${message}">Hello World!</h1> </body> </html> ``` 这个示例使用Thymeleaf的th:text标签将一个变量message的值输出到页面上。 4. 创建控制器类 创建一个控制器类,处理HTTP请求并返回Thymeleaf模板。例如: ``` @Controller public class HomeController { @GetMapping("/") public String home(Model model) { model.addAttribute("message", "Hello from Thymeleaf!"); return "home"; } } ``` 这个示例中,控制器类处理根路径的GET请求,并将一个名为message的变量添加到模型中。最后,返回一个名为“home”的字符串,它与Thymeleaf模板的名称对应。 5. 运行应用程序 运行应用程序并在浏览器中打开http://localhost:8080/,您将看到Thymeleaf渲染的页面,其中包含控制器方法中添加的消息。 到这里,您就成功地搭建了Spring Boot使用Thymeleaf视图模板进行Web开发的环境。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值