欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot+thymeleaf+ajax實(shí)現(xiàn)局部刷新詳情

 更新時(shí)間:2022年09月23日 11:42:48   作者:晚上睡不著_???????  
這篇文章主要介紹了SpringBoot+thymeleaf+ajax實(shí)現(xiàn)局部刷新詳情,文章圍繞主題展開詳細(xì)的內(nèi)容介紹具有一定的參考價(jià)值,需要的小伙伴可以參考一下

前言

什么是局部刷新?

簡(jiǎn)而言之,就是當(dāng)我發(fā)送一個(gè)請(qǐng)求到后端后拿到數(shù)據(jù)后返回當(dāng)前 頁(yè)面不會(huì)對(duì)整個(gè)頁(yè)面進(jìn)行重載而只對(duì)當(dāng)前請(qǐng)求的模塊進(jìn)行刷新。

優(yōu)勢(shì)和弊端?

優(yōu)勢(shì):

  • 用戶體驗(yàn)好,不需要對(duì)頁(yè)面進(jìn)行重載
  • 利于開發(fā)人員開發(fā),提高開發(fā)效率
  • 前后端完全分離

弊端:

  • 不利于優(yōu)化!
    第一次從服務(wù)器端獲取的內(nèi)容不包含需要?jiǎng)討B(tài)綁定的數(shù)據(jù),所以頁(yè)面的源代碼中沒有這些內(nèi)容,不利于收錄,后期通過js添加到頁(yè)面中的內(nèi)容,并不會(huì)寫在頁(yè)面的源代碼中~
  • 時(shí)間上的些許浪費(fèi),沒有服務(wù)器端渲染頁(yè)面呈現(xiàn)速度快!
    交由客戶端渲染,首先需要把頁(yè)面呈現(xiàn),然后再通過js的異步ajax請(qǐng)求獲取數(shù)據(jù),然后數(shù)據(jù)綁定,瀏覽器再把動(dòng)態(tài)增加的部分重新渲染,這樣就浪費(fèi)了一些時(shí)間,沒有服務(wù)器端渲染頁(yè)面速度快!?。?/li>

實(shí)現(xiàn)流程

  • 通過前端一部請(qǐng)求到后端接口
  • 通過模板語(yǔ)法 返回頁(yè)面: : 刷新的標(biāo)記
  • 前端渲染頁(yè)面 th:fragment="刷新的標(biāo)記"

案列

首先我們需要先右鍵new一個(gè)springboot項(xiàng)目

配置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實(shí)現(xiàn)局部刷新測(cè)試接口類
 * @Create: 2022-07-24 0:29
 * @Version 1.0
 **/
@Controller
public class WebController {
    /**
     * @methodName: list
     * @description: 測(cè)試接口
     * @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包下新建一個(gè)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>

到此這篇關(guān)于SpringBoot+thymeleaf+ajax實(shí)現(xiàn)局部刷新詳情的文章就介紹到這了,更多相關(guān)SpringBoot thymeleaf 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • springboot連接不同數(shù)據(jù)庫(kù)的寫法詳解

    springboot連接不同數(shù)據(jù)庫(kù)的寫法詳解

    這篇文章主要介紹了springboot連接不同數(shù)據(jù)庫(kù)的寫法?,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-04-04
  • SpringBoot內(nèi)置tomcat參數(shù)調(diào)優(yōu)的實(shí)現(xiàn)

    SpringBoot內(nèi)置tomcat參數(shù)調(diào)優(yōu)的實(shí)現(xiàn)

    springboot內(nèi)置了tomcat, 并給我們?cè)O(shè)置了默認(rèn)參數(shù), 我們?cè)趺礃有薷膕pringboot內(nèi)置的tomcat參數(shù),本文就詳細(xì)的來(lái)介紹一下,感興趣的可以了解一下
    2023-09-09
  • SpringBoot入門之集成JSP的示例代碼

    SpringBoot入門之集成JSP的示例代碼

    這篇文章主要介紹了SpringBoot入門之集成JSP的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧
    2018-07-07
  • java_String和StringBuffer區(qū)別分析

    java_String和StringBuffer區(qū)別分析

    JAVA平臺(tái)提供了兩個(gè)類:String和StringBuffer,它們可以儲(chǔ)存和操作字符串,即包含多個(gè)字符的字符數(shù)據(jù)。這個(gè)String類提供了數(shù)值不可改變的字符串。
    2013-04-04
  • Java常用時(shí)間工具類總結(jié)(珍藏版)

    Java常用時(shí)間工具類總結(jié)(珍藏版)

    這篇文章主要為大家詳細(xì)介紹了Java中一些常用時(shí)間工具類的使用示例代碼,文中的代碼簡(jiǎn)潔易懂,對(duì)我們學(xué)習(xí)Java有一定幫助,需要的可以參考一下
    2022-07-07
  • Java Map 按照Value排序的實(shí)現(xiàn)方法

    Java Map 按照Value排序的實(shí)現(xiàn)方法

    Map是鍵值對(duì)的集合接口,它的實(shí)現(xiàn)類主要包括:HashMap,TreeMap,Hashtable以及LinkedHashMap等。這篇文章主要介紹了Java Map 按照Value排序的實(shí)現(xiàn)方法,需要的朋友可以參考下
    2016-08-08
  • SpringCloud?Gateway中GatewayFilterChain執(zhí)行流程詳解

    SpringCloud?Gateway中GatewayFilterChain執(zhí)行流程詳解

    Spring?Cloud?Gateway旨在為微服務(wù)架構(gòu)提供一種簡(jiǎn)單有效的、統(tǒng)一的?API?路由管理方式。Spring?Cloud?Gateway?作為?Spring?Cloud?生態(tài)系中的網(wǎng)關(guān),它不僅提供統(tǒng)一的路由方式,并且基于?Filter?鏈的方式提供了網(wǎng)關(guān)基本的功能,例如:安全、監(jiān)控/埋點(diǎn)和限流等
    2022-10-10
  • java后臺(tái)利用Apache poi 生成excel文檔提供前臺(tái)下載示例

    java后臺(tái)利用Apache poi 生成excel文檔提供前臺(tái)下載示例

    本篇文章主要介紹了java后臺(tái)利用Apache poi 生成excel文檔提供前臺(tái)下載示例,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2017-05-05
  • Java程序員必備的11大IntelliJ插件(附地址)

    Java程序員必備的11大IntelliJ插件(附地址)

    這篇文章主要介紹了Java程序員必備的11大IntelliJ插件(附地址),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • 在java中 利用匿名內(nèi)部類進(jìn)行較簡(jiǎn)潔的雙括弧初始化的方法

    在java中 利用匿名內(nèi)部類進(jìn)行較簡(jiǎn)潔的雙括弧初始化的方法

    本篇文章小編將為大家介紹,關(guān)于在java中 利用匿名內(nèi)部類進(jìn)行較簡(jiǎn)潔的雙括弧初始化的方法,有需要的朋友可以參考一下
    2013-04-04

最新評(píng)論