用户登录

1.代码

1.1后端

java 复制代码
package com.bite.springmvc;

import ch.qos.logback.core.util.StringUtil;
import jakarta.servlet.http.HttpSession;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/user")
@RestController
public class UserController {
@RequestMapping("/login")
    public boolean login(String userName, String password, HttpSession session){
    if (!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)){
        return false;
    }
    //校验账号和密码
    //当前还未学习数据库的操作,先写死
    if ("zhangsan".equals(userName) && "123456".equals(password)){
        //存储Session
        session.setAttribute("userName", userName);
        return true;
    }
    return false;
    }

    @RequestMapping("/getUserInfo")
    public String getUserInfo(HttpSession session){
    String userName = (String) session.getAttribute("userName");
    return userName==null?"":userName;
    }
}

1.2前端

login

html 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>登录页面</title>
</head>

<body>
  <h1>用户登录</h1>
  用户名:<input name="userName" type="text" id="userName"><br>
  密码:<input name="password" type="password" id="password"><br>
  <input type="button" value="登录" onclick="login()">
  
  <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
  <script>
    function login() {
    $.ajax({
      url:"/user/login",
      type:"post",
      data:{
        "userName":$("#userName").val(),
        "password":$("#password").val()
      },
      success:function(result){
        //返回结果为true,页面跳转
        if(result){
          location.href = "index.html";
        }else{
          //密码错误
          alert("密码错误")
        }
      }
    });
    }

  </script>
</body>

</html>

index

java 复制代码
<!doctype html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>用户登录首页</title>
</head>

<body>
    登录人: <span id="loginUser"></span>

    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
    <script>
        $.ajax({
            url:"/user/getUserInfo",
            type:"get",
            success:function(result){
                //把result放在loginUser这个spn下面
                $("#loginUser").text(result);
            }
        });
    </script>
</body>

</html>

2.测试

密码错误时:

登录成功时

相关推荐
深漂的华哥23 分钟前
Ruoyi-Plus前后端分离场景下,数据加密传输
java·spring boot·后端·开源·maven·ruoyi
剑胆琴心静水深流2 小时前
全栈之路6---web集成与呈现
前端·vue.js·spring boot·分布式·spring·前端框架·npm
qq_185198695 小时前
SpringBoot-五-AOT
java·spring boot·后端
卓怡学长6 小时前
w180springboot基于Java的悠扬乐器管理
java·spring boot·mysql·spring·maven·intellij-idea
choumou_M7 小时前
SpringBoot_5:商品秒杀场景的并发实践(初步)
java·spring boot·后端
攻城有术8 小时前
专项攻克-SpringBoot启动后 Bean的实例数量问题
java·spring boot·后端
程序员贺加贝9 小时前
别把 ThreadLocal 写进 Serializer:一次 SaaS 多租户 Jackson 上下文污染排查
java·spring boot
evans在进步10 小时前
Spring Boot 工程化核心详解:Parent、Starter、热部署、事务与多数据源
spring boot·后端·python
卓怡学长10 小时前
w181springboot机场乘客服务系统
java·数据库·spring boot·spring·intellij-idea
罗超驿11 小时前
SpringBoot 快速入门
java·spring boot·后端