網站首頁 編程語言 正文
簡介
本文用示例展示簡單的登錄頁面的寫法。
會包括如下幾種方案:純HTML、HTML+jQuery(form data)格式、HTML+jQuery(json)格式。
公共代碼(后端接口)
用SpringBoot寫一個最簡單的登錄接口。
Controller
package com.example.controller; import com.example.entity.LoginVO; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; //跨域 @CrossOrigin //Rest風格:返回JSON @RestController public class LoginController { @PostMapping("login") public LoginVO login() { //省略對用戶名和密碼的判斷 LoginVO loginVO = new LoginVO(); loginVO.setSuccess(true); loginVO.setData("This is data"); return loginVO; } }
pom.xml
<?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.3.0.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo_SpringBoot</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo_SpringBoot</name> <description>Demo project for Spring Boot</description> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
示例1:最簡(純HTML)
代碼
login.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>登錄頁</title> </head> <body> <form action="http://localhost:8080/login" method="post"> <label for="username">用戶名:</label> <input type="text" name="username" id="username"> <label for="password">密碼:</label> <input type="password" name="password" id="password"> <!--下邊這樣寫也可以 <label for="username"> 用戶名:<input type="text" name="username" id="username"> </label> <label for="password"> 密碼:<input type="password" name="password" id="password"> </label>--> <button type="submit">登錄</button> </form> </body> </html>
測試
1.訪問login.html
2.輸入用戶名和密碼
用戶名:輸入abc;密碼:輸入 1234
結果
示例2:HTML+jQuery(form data)
代碼
login.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>登錄頁</title> <script src="https://cdn.staticfile.org/jquery/1.11.3/jquery.min.js"></script> </head> <body> <form id="login-form"> <label for="username">用戶名:</label> <input type="text" name="username" id="username"> <label for="password">密碼:</label> <input type="password" name="password" id="password"> </form> <div id="error-message"></div> <button type="submit" onclick="loginViaFormData()">登錄</button> <script> function loginViaFormData() { $.ajax( { type: "post", url: "http://localhost:8080/login", data: $("#login-form").serialize(), // 序列化form表單里面的數據傳到后臺 //dataType: "json", // 指定后臺傳過來的數據是json格式 success: function (result) { if (!result.success) { $("#errormessage").text("用戶名或密碼錯誤"); } else if (result.success) { alert("登錄成功"); // 跳到index.html頁面 window.location.href="index.html" rel="external nofollow" rel="external nofollow" ; } } } ) } </script> </body> </html>
index.html
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>This is title</title> </head> <body> <div class="container"> 登錄成功后的頁面 </div> <script> </script> </body> </html>
測試
1.訪問login.html
2.輸入用戶名和密碼
用戶名:輸入abc;密碼:輸入 1234
3.點擊登錄
4.點擊確定
示例3:HTML+jQuery(json)
代碼
login.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>登錄頁</title> <script src="https://cdn.staticfile.org/jquery/1.11.3/jquery.min.js"></script> </head> <body> <form id="login-form"> <label for="username">用戶名:</label> <input type="text" name="username" id="username"> <label for="password">密碼:</label> <input type="password" name="password" id="password"> </form> <div id="error-message"></div> <button type="submit" onclick="loginViaJson()">登錄</button> <script> function loginViaJson() { $.post("http://localhost:8080/login", //發送給后端的數據 { "userName": $(".username").val(), "password": $(".password").val() }, //回調函數 function (result) { if (!result.success) { $("#errormessage").text("用戶名或密碼錯誤"); } else if (result.success) { alert("登錄成功"); // 跳到index.html頁面 window.location.href="index.html" rel="external nofollow" rel="external nofollow" ; } } ) } </script> </body> </html>
index.html
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>This is title</title> </head> <body> <div class="container"> 登錄成功后的頁面 </div> <script> </script> </body> </html>
測試
測試結果和前邊“示例2:HTML+jQuery(form data)”一樣
1.訪問login.html
2.輸入用戶名和密碼
用戶名:輸入abc;密碼:輸入 1234
3.點擊登錄
4.點擊確定
原文鏈接:https://knife.blog.csdn.net/article/details/121867005
相關推薦
- 2022-11-21 C++獲取文件大小數值的三種方式介紹_C 語言
- 2022-09-05 Python?Behave框架學習_python
- 2022-04-03 django8.5?項目部署Nginx的操作步驟_nginx
- 2022-08-12 C++鏈表實現通訊錄設計_C 語言
- 2022-01-27 修改Linux系統時間Date改為CST
- 2022-07-15 Android自定義view實現圓形進度條效果_Android
- 2022-03-28 Python實現網頁文件轉PDF文件和PNG圖片的示例代碼_python
- 2021-12-12 七大經典排序算法圖解_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支