HTML简单的登录页面--实例

编程知识 行业动态 更新时间:2024-06-13 00:17:58

原文网址:HTML简单的登录页面--实例_IT利刃出鞘的博客-CSDN博客

简介

        本文用示例展示简单的登录页面的写法。

        会包括如下几种方案:纯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/POM/4.0.0" xmlns:xsi="http://www.w3/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache/POM/4.0.0 https://maven.apache/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>-->

    <!--必须是submit类型。如果是button类型,点击后不会请求-->
    <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/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">

    <!--下边这样写也可以
    <label for="username">
        用户名:<input type="text" name="username" id="username">
    </label>

    <label for="password">
        密码:<input type="password" name="password" id="password">
    </label>-->
</form>

<div id="error-message"></div>
<button 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";
                    }
                }
            }
        )
    }
</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/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">

    <!--下边这样写也可以
    <label for="username">
        用户名:<input type="text" name="username" id="username">
    </label>

    <label for="password">
        密码:<input type="password" name="password" id="password">
    </label>-->
</form>

<div id="error-message"></div>
<button 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";
                }
            }
        )
    }
</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.点击确定

其他网址

基于Jquery实现登录功能的前端页面_阳菜-CSDN博客_jquery登录页面

web登录页面jquery校验的简单实现(一)_Mr.wang的博客-CSDN博客_jquery登录页面验证

更多推荐

HTML简单的登录页面--实例

本文发布于:2023-03-26 02:23:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/aad0e75b83806e518a5296116f2d28bb.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实例   页面   简单   HTML

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!