千峰商城-springboot项目实战06-Thymeleaf

追求适度,才能走向成功;人在顶峰,迈步就是下坡;身在低谷,抬足既是登高;弦,绷得太紧会断;人,思虑过度会疯;水至清无鱼,人至真无友,山至高无树;适度,不是中庸,而是一种明智的生活态度。

导读:本篇文章讲解 千峰商城-springboot项目实战06-Thymeleaf,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

Thymeleaf是一种类似于JSP的动态网页技术。

1.Thymeleaf简介

JSP必须依赖于Tomcat运行,不能直接运行在浏览器中。

HTML可以直接运行在浏览器中,但不能接收控制器传递的数据。

Thymeleaf是一种既保留了HTML的后缀,能够直接在浏览器运行的能力,又实现了JSP显示动态数据的功能——静能查看页面效果,动则可以显示数据。

2.Thymeleaf动态技术的使用:

springboot应用对Thymeleaf提供了良好的支持。

1.添加Thymeleaf的starter依赖

在pom.xml中添加依赖:spring-boot-starter-thymeleaf

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

2.创建Thymeleaf模板。

Thymeleaf模板就是HTML文件。

springboot应用中,在 resource\templates 目录就是用来存放页面模板的。

在 resource\templates 目录下新建一个HTML文件 test.html

千峰商城-springboot项目实战06-Thymeleaf

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
test
</body>
</html>

在 resource\static 目录下新建一个HTML文件 test2.html

千峰商城-springboot项目实战06-Thymeleaf

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
test2
</body>
</html>

3.测试

在浏览器中访问:http://localhost:8081/test2.html

千峰商城-springboot项目实战06-Thymeleaf

因为test2.html在tatic静态资源里,所以可以访问。

而访问 http://localhost:8081/test.html 时:

千峰商城-springboot项目实战06-Thymeleaf

 访问不到。因为除了static文件夹可以放行外,其余文件夹都不能放行。

说明:static目录下的资源被定义为静态资源,springboot应用默认放行。如果将HTML页面创建在static目录下,是可以直接访问的。

   templates目录下的文件会被定义为动态网页模板,springboot应用会拦截templates中定义的资源,如果将HTML文件定义在templates目录,则必须通过控制器跳转访问。

4.在com.qfedu.springboot.ssm文件下新建一个包controller,然后在controller包中新建一个类PageController,用于转发允许“直接访问”的页面请求。

千峰商城-springboot项目实战06-Thymeleaf

 PageController.java:

package com.qfedu.springboot.ssm.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
//@RequestMapping("/page")
public class PageController {
    
    @RequestMapping("/test.html")
    public String test(){
        return "test";
    }
    
}

5.启动

访问 http://localhost:8081/test.html

千峰商城-springboot项目实战06-Thymeleaf

 实际情况:请求“test.html”被spring mvc拦截了,转发到了PageController中,PageController又将请求跳转到了test.html中。

  所以 templates目录下的文件 都必须被controller转发。

 6.Thymeleaf 服务器可以不启动,直接查看页面样式。

千峰商城-springboot项目实战06-Thymeleaf

 千峰商城-springboot项目实战06-Thymeleaf

 7.模拟传输数据到test.html

在controller包中创建BookController类

千峰商城-springboot项目实战06-Thymeleaf

package com.qfedu.springboot.ssm.controller;

import com.qfedu.springboot.ssm.beans.Book;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/book")
public class BookController {

    @RequestMapping("/query")
    public String queryBook(int bookId, Model model){
        Book book = new Book(bookId,"java","author");
        model.addAttribute("book",book);
        return "test";
    }

}

在Beans包中创建Book实体类

千峰商城-springboot项目实战06-Thymeleaf

package com.qfedu.springboot.ssm.beans;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Book {
    private int bookId;
    private String bookName;
    private String bookAuthor;
}

在test.html中:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
test
<hr/>
<label th:text="${book.bookName}">图书名称</label>
</body>
</html>

8.观看静态界面:

千峰商城-springboot项目实战06-Thymeleaf

 运行动态界面:

http://localhost:8081/book/query?bookId=1

千峰商城-springboot项目实战06-Thymeleaf

 获取到了bookName:java。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/128189.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!