用户登录

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.测试

密码错误时:

登录成功时

相关推荐
一 乐7 小时前
婚纱摄影网站|基于ssm + vue婚纱摄影网站系统(源码+数据库+文档)
前端·javascript·数据库·vue.js·spring boot·后端
期待のcode9 小时前
前后端分离项目 Springboot+vue 在云服务器上的部署
服务器·vue.js·spring boot
ProgramHan10 小时前
Spring Boot 3.2 新特性:虚拟线程的落地实践
java·jvm·spring boot
源码获取_wx:Fegn089511 小时前
基于 vue智慧养老院系统
开发语言·前端·javascript·vue.js·spring boot·后端·课程设计
毕设源码_郑学姐11 小时前
计算机毕业设计springboot基于HTML5的酒店预订管理系统 基于Spring Boot框架的HTML5酒店预订管理平台设计与实现 HTML5与Spring Boot技术驱动的酒店预订管理系统开
spring boot·后端·课程设计
不吃香菜学java12 小时前
spring-依赖注入
java·spring boot·后端·spring·ssm
南部余额12 小时前
Spring Boot 整合 MinIO:封装常用工具类简化文件上传、启动项目初始化桶
java·spring boot·后端·文件上传·工具类·minio·minioutils
QQ196328847512 小时前
ssm基于Springboot+的球鞋销售商城网站vue
vue.js·spring boot·后端
太空眼睛12 小时前
【MCP】使用SpringBoot基于Streamable-HTTP构建MCP-Server
spring boot·sse·curl·mcp·mcp-server·spring-ai·streamable
幽络源小助理12 小时前
springboot校园车辆管理系统源码 – SpringBoot+Vue项目免费下载 | 幽络源
vue.js·spring boot·后端