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

Spring Boot管理用戶數(shù)據(jù)的操作步驟

 更新時間:2024年09月23日 15:24:40   作者:布說在見  
SpringBoot結(jié)合Thymeleaf模板引擎,可以快速搭建Web應(yīng)用,介紹了使用SpringBoot處理JSON數(shù)據(jù)的基本過程,包括創(chuàng)建實體類、視圖頁面和控制器,通過這些步驟,即可完成基于SpringBoot和Thymeleaf的簡單Web開發(fā),感興趣的朋友跟隨小編一起看看吧

學習目標

Spring Boot 的 Web 的開發(fā)

Thymeleaf 模板JSON 數(shù)據(jù)

前言

前端開發(fā),也稱為客戶端開發(fā),專注于用戶界面和用戶體驗。后端開發(fā),或服務(wù)器端開發(fā),處理服務(wù)器、應(yīng)用程序和數(shù)據(jù)庫的邏輯。Web開發(fā)可以分為兩大主要領(lǐng)域:前端開發(fā)和后端開發(fā)

Thymeleaf 模板JSON 數(shù)據(jù)

Thymeleaf 是一個的Java模板引擎,具備生成多種文本格式的能力,包括HTML、XML、JavaScript、CSS以及簡單的純文本。

創(chuàng)建一個基于 Thymeleaf 模板的 Spring Boot Web 應(yīng)用程序相對簡單。

步驟 1: 創(chuàng)建 Spring Boot 項目

首先,你需要創(chuàng)建一個新的 Spring Boot 項目。你可以通過 Spring Initializr 網(wǎng)站來創(chuàng)建,或者使用 IDE 如 IntelliJ IDEA 或 Eclipse。

使用 Spring Initializr 創(chuàng)建項目

訪問 Spring Initializr。選擇項目元數(shù)據(jù): Project:Maven Project 或 Gradle ProjectLanguage:JavaPackaging:JarJava:選擇你安裝的 Java 版本 添加依賴項: Spring WebThymeleaf 選擇默認的其他選項,點擊生成按鈕下載項目。

我嘗試更換Java版本,在IDEA中使用JDK1.8時一切正常,但當我切換到JDK17時遇到了錯誤。

為了解決這個問題,我選擇了一個曲線救國的方法:回退版本。

我將父類依賴從3.3.4降低到了2.6.4。(避免報錯,在我的電腦)

使用 IDE 創(chuàng)建項目

如果你使用的是支持 Spring Boot 的 IDE,可以在 IDE 中新建一個 Spring Boot 項目,并添加上述依賴。

步驟 2: 添加依賴

如果你使用的是 Maven,確保在 pom.xml 文件中添加以下依賴:

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

步驟 3: 創(chuàng)建 Controller

接下來,創(chuàng)建一個簡單的控制器來處理 HTTP 請求:

package com.bushuo.demo01.controller;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class TestThymeleafController {    @RequestMapping("/index")    public String test(){    //返回src/main/resources/templates/index.html        return "index";    }}

步驟 4: 新建index頁面

src/main/resources/templates 目錄下創(chuàng)建一個名為 index.html 的文件,并添加以下內(nèi)容:

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>Title</title></head><body><h1 >Welcome to Spring Boot!</h1></body></html>

步驟 5: 運行應(yīng)用程序

現(xiàn)在,你可以運行你的 Spring Boot 應(yīng)用了。在終端或命令提示符中,導航到項目的根目錄并執(zhí)行以下命令:

mvn spring-boot:run

或者在 IDE 中直接運行主類(通常包含 @SpringBootApplication 注解)。

表單提交

在 Spring Boot 應(yīng)用程序中使用 Thymeleaf 實現(xiàn)表單提交及數(shù)據(jù)綁定的過程主要包括以下幾個步驟:

添加 Thymeleaf 依賴創(chuàng)建 Controller 處理表單請求創(chuàng)建 Thymeleaf 模板文件運行應(yīng)用程序

步驟 1: 添加 Thymeleaf 依賴

確保項目已經(jīng)包含了 Thymeleaf 依賴。如果你還沒有添加,請參考之前的指導在 pom.xml 中添加相應(yīng)的依賴。

在 Maven 中添加依賴

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

步驟2:創(chuàng)建實體類

在src/main/java 目錄下,創(chuàng)建model包,創(chuàng)建實體類loginBean

package com.bushuo.demo01.model;public class LoginBean {    String name;    String role;  public LoginBean() {    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getRole() {        return role;    }    public void setRole(String role) {        this.role = role;    }}

步驟 3: 創(chuàng)建 Controller 處理表單請求

創(chuàng)建一個控制器來處理表單,并實現(xiàn)數(shù)據(jù)綁定。

步驟 3: 創(chuàng)建 Thymeleaf 模板文件

在src/main/resources/templates 目錄下
創(chuàng)建兩個 Thymeleaf 模板文件:一個用于顯示表單 (login.html),另一個用于展示提交后的結(jié)果 (result.html)。

login.html

result.html

這個模板用于展示表單提交的結(jié)果,并提供一個返回按鈕讓用戶回到表單頁面。
在application.yml,

spring:  thymeleaf:    encoding: UTF-8    #開發(fā)時關(guān)閉緩存(模板只加載一次),不然沒法看到實時頁面需要重構(gòu)或重啟    cache: false    prefix: classpath:/templates/  #(默認)    suffix: .html

步驟 4: 運行應(yīng)用程序

運行你的 Spring Boot 應(yīng)用程序,打開瀏覽器并訪問 http://localhost:8080/toLogin,你應(yīng)該能看到表單頁面。填寫用戶名和電子郵件地址并提交表單后,你會看到結(jié)果頁面,并且可以通過返回按鈕回到表單頁面。

Spring Boot 處理JSON 數(shù)據(jù)的過程

1.創(chuàng)建實體類

在com.bushuo.demo01包中,創(chuàng)建一個實體類來表示要處理的 JSON 數(shù)據(jù)。例如,創(chuàng)建一個 Person 類:

2.創(chuàng)建視圖頁面

在src/main/resources/templates 目錄下,創(chuàng)建視圖頁面input.html.

3.控制器

4.運行

總結(jié)

通過這些步驟,可以在 Spring Boot 應(yīng)用程序中使用 Thymeleaf 創(chuàng)建一個簡單的表單,并處理表單提交的數(shù)據(jù)。可以根據(jù)實際需求進一步擴展這個示例,例如添加更多的表單字段、進行更復雜的表單驗證、使用數(shù)據(jù)庫存儲數(shù)據(jù)等。

到此這篇關(guān)于Spring Boot管理用戶數(shù)據(jù)的操作步驟的文章就介紹到這了,更多相關(guān)Spring Boot用戶數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論