網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
簡(jiǎn)介
本文用示例展示簡(jiǎn)單的登錄頁(yè)面的寫(xiě)法。
會(huì)包括如下幾種方案:純HTML、HTML+jQuery(form data)格式、HTML+jQuery(json)格式。
公共代碼(后端接口)
用SpringBoot寫(xiě)一個(gè)最簡(jiǎn)單的登錄接口。
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風(fēng)格:返回JSON @RestController public class LoginController { @PostMapping("login") public LoginVO login() { //省略對(duì)用戶名和密碼的判斷 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:最簡(jiǎn)(純HTML)
代碼
login.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>登錄頁(yè)</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"> <!--下邊這樣寫(xiě)也可以 <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>
測(cè)試
1.訪問(wèn)login.html
2.輸入用戶名和密碼
用戶名:輸入abc;密碼:輸入 1234
結(jié)果
示例2:HTML+jQuery(form data)
代碼
login.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>登錄頁(yè)</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表單里面的數(shù)據(jù)傳到后臺(tái) //dataType: "json", // 指定后臺(tái)傳過(guò)來(lái)的數(shù)據(jù)是json格式 success: function (result) { if (!result.success) { $("#errormessage").text("用戶名或密碼錯(cuò)誤"); } else if (result.success) { alert("登錄成功"); // 跳到index.html頁(yè)面 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"> 登錄成功后的頁(yè)面 </div> <script> </script> </body> </html>
測(cè)試
1.訪問(wèn)login.html
2.輸入用戶名和密碼
用戶名:輸入abc;密碼:輸入 1234
3.點(diǎn)擊登錄
4.點(diǎn)擊確定
示例3:HTML+jQuery(json)
代碼
login.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>登錄頁(yè)</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", //發(fā)送給后端的數(shù)據(jù) { "userName": $(".username").val(), "password": $(".password").val() }, //回調(diào)函數(shù) function (result) { if (!result.success) { $("#errormessage").text("用戶名或密碼錯(cuò)誤"); } else if (result.success) { alert("登錄成功"); // 跳到index.html頁(yè)面 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"> 登錄成功后的頁(yè)面 </div> <script> </script> </body> </html>
測(cè)試
測(cè)試結(jié)果和前邊“示例2:HTML+jQuery(form data)”一樣
1.訪問(wèn)login.html
2.輸入用戶名和密碼
用戶名:輸入abc;密碼:輸入 1234
3.點(diǎn)擊登錄
4.點(diǎn)擊確定
原文鏈接:https://knife.blog.csdn.net/article/details/121867005
相關(guān)推薦
- 2022-10-13 react-router?v6實(shí)現(xiàn)動(dòng)態(tài)路由實(shí)例_React
- 2022-11-15 Apache?Doris的Bitmap索引和BloomFilter索引使用及注意事項(xiàng)_Linux
- 2022-07-13 復(fù)選框 prop 不觸發(fā) change 事件
- 2023-02-04 Android自定義view實(shí)現(xiàn)雪花特效實(shí)例代碼_Android
- 2022-06-29 tomcat正常啟動(dòng)但網(wǎng)頁(yè)卻無(wú)法訪問(wèn)的幾種解決方法_Tomcat
- 2022-03-07 android?studio?項(xiàng)目?:UI設(shè)計(jì)高精度實(shí)現(xiàn)簡(jiǎn)單計(jì)算器_Android
- 2022-07-13 ELK 日志分析系統(tǒng)的部署
- 2023-10-15 centos 切換g++
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支