SpringBoot+thymeleaf+ajax實現(xiàn)局部刷新詳情
前言
什么是局部刷新?
簡而言之,就是當我發(fā)送一個請求到后端后拿到數(shù)據(jù)后返回當前 頁面不會對整個頁面進行重載而只對當前請求的模塊進行刷新。
優(yōu)勢和弊端?
優(yōu)勢:
- 用戶體驗好,不需要對頁面進行重載
- 利于開發(fā)人員開發(fā),提高開發(fā)效率
- 前后端完全分離
弊端:
- 不利于優(yōu)化!
第一次從服務器端獲取的內(nèi)容不包含需要動態(tài)綁定的數(shù)據(jù),所以頁面的源代碼中沒有這些內(nèi)容,不利于收錄,后期通過js添加到頁面中的內(nèi)容,并不會寫在頁面的源代碼中~ - 時間上的些許浪費,沒有服務器端渲染頁面呈現(xiàn)速度快!
交由客戶端渲染,首先需要把頁面呈現(xiàn),然后再通過js的異步ajax請求獲取數(shù)據(jù),然后數(shù)據(jù)綁定,瀏覽器再把動態(tài)增加的部分重新渲染,這樣就浪費了一些時間,沒有服務器端渲染頁面速度快?。?!
實現(xiàn)流程
- 通過前端一部請求到后端接口
- 通過模板語法 返回頁面: : 刷新的標記
- 前端渲染頁面
th:fragment="刷新的標記"
案列
首先我們需要先右鍵new一個springboot項目
配置pom依賴:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.2</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>demo</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
配置yml文件
server: port: 8080 # Spring配置 spring: # 模板引擎 thymeleaf: # 禁用緩存 cache: false prefix: classpath:/templates/ suffix: .html mode: HTML encoding: utf-8
編寫接口:
package com.example.demo.web; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import java.util.ArrayList; import java.util.List; /** * @Program: demo * @ClassName WebController * @Author: LiuTao * @Description: SpringBoot+thymeleaf+ajax實現(xiàn)局部刷新測試接口類 * @Create: 2022-07-24 0:29 * @Version 1.0 **/ @Controller public class WebController { /** * @methodName: list * @description: 測試接口 * @Author LiuTao * @param [model] * @updateTime 2022/7/24 0:33 * @return java.lang.String * @throws **/ @RequestMapping("/list") public String list(Model model) { List lists = new ArrayList(); lists.add("aaaa"); lists.add("bbbb"); lists.add("cccc"); lists.add("dddd"); model.addAttribute("list",lists); return "index::list"; } }
在templates包下新建一個index.html
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8" /> <title>Title</title> <script th:src="@{/jquery.min.js}"></script> </head> <body> <button onclick="list()">獲取列表</button> <table border="1" th:fragment="list" id="list" > <thead> <th>用戶</th> </thead> <tr th:each="list:${list}"> <td>[[${list}]]</td> </tr> </table> </body> <script> function list(){ //第一種方法 // $('#search').load("/list"); //第二種方法 $.ajax({ url: "/list", type: 'POST', success: function (data) { $("#list").html(data); } }) } </script> </html>
到此這篇關于SpringBoot+thymeleaf+ajax實現(xiàn)局部刷新詳情的文章就介紹到這了,更多相關SpringBoot thymeleaf 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
springboot連接不同數(shù)據(jù)庫的寫法詳解
這篇文章主要介紹了springboot連接不同數(shù)據(jù)庫的寫法?,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-04-04SpringBoot內(nèi)置tomcat參數(shù)調(diào)優(yōu)的實現(xiàn)
springboot內(nèi)置了tomcat, 并給我們設置了默認參數(shù), 我們怎么樣修改springboot內(nèi)置的tomcat參數(shù),本文就詳細的來介紹一下,感興趣的可以了解一下2023-09-09java_String和StringBuffer區(qū)別分析
JAVA平臺提供了兩個類:String和StringBuffer,它們可以儲存和操作字符串,即包含多個字符的字符數(shù)據(jù)。這個String類提供了數(shù)值不可改變的字符串。2013-04-04SpringCloud?Gateway中GatewayFilterChain執(zhí)行流程詳解
Spring?Cloud?Gateway旨在為微服務架構提供一種簡單有效的、統(tǒng)一的?API?路由管理方式。Spring?Cloud?Gateway?作為?Spring?Cloud?生態(tài)系中的網(wǎng)關,它不僅提供統(tǒng)一的路由方式,并且基于?Filter?鏈的方式提供了網(wǎng)關基本的功能,例如:安全、監(jiān)控/埋點和限流等2022-10-10java后臺利用Apache poi 生成excel文檔提供前臺下載示例
本篇文章主要介紹了java后臺利用Apache poi 生成excel文檔提供前臺下載示例,非常具有實用價值,需要的朋友可以參考下2017-05-05在java中 利用匿名內(nèi)部類進行較簡潔的雙括弧初始化的方法
本篇文章小編將為大家介紹,關于在java中 利用匿名內(nèi)部類進行較簡潔的雙括弧初始化的方法,有需要的朋友可以參考一下2013-04-04