当前位置: 首页 > article >正文

JavaEE 实现 登录+注册(采用注解方式链接数据库)

(Spring MVC的Controller练习)

工具:Tomcat 10.0.23,MySQL,JDK18

一、运行效果展示

点击运行Tomcat首先进入index.jsp页面

若已有账号点击登录即可进行登录,这里先点击“获取ROY6账号”去注册,进入register.jsp页面

注册成功后跳转到success.jsp页面,此时数据库中user表会增加一条用户信息

点击“前往探索ROY6商城此时,此时为login.jsp页面

输入已经注册过的账号,登录,进入welcome.jsp页面

这里我简单的写了两个小功能,就不进行展示了,本文主要展示登录注册功能。

下面进入详细操作步骤,请慢慢食用代码!

二、项目代码

先查看项目结构图:

红色框出部分为本次实验用到代码

 

1、建立数据库,建表

首先创建数据库spring_db,然后建立所需要的用户表user

CREATE DATABASE IF NOT EXISTS spring_db CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;

USE spring_db;

CREATE TABLE user (
                      id INT AUTO_INCREMENT PRIMARY KEY,
                      username VARCHAR(50) NOT NULL UNIQUE,
                      password VARCHAR(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

 2、创建第一个index.jsp页面

页面主要包含注册以及登陆,很简单,直接放代码

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>欢迎</title>
    <style>
        /* 页面背景 */
        body {
            font-family: 'Arial', sans-serif;
            margin: 0;
            padding: 0;
            background: url('https://wx3.sinaimg.cn/mw690/007doFNOgy1hf8lzl0rs2j323u16q7wh.jpg') no-repeat center center fixed;
            background-size: cover;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            overflow: hidden;
            animation: backgroundAnimation 10s infinite alternate;
        }

        /* 页面内容容器 */
        .container {
            text-align: center;
            background-color: white;
            padding: 40px;
            border-radius: 12px;
            box-shadow: 0 6px 15px rgba(0, 0, 0, 0.1); /* 阴影效果 */
            background-color: rgba(255, 255, 255, 0.95); /* 背景半透明 */
            width: 100%;
            max-width: 500px;
        }

        /* 标题样式 */
        h3 {
            font-family: 'Comic Sans MS', cursive, sans-serif; /* 卡通风格字体 */
            font-size: 28px;
            color: #00BFFF; /* 天青色 */
            margin-bottom: 20px;
        }

        /* 链接列表样式 */
        ul {
            list-style-type: none;
            padding: 0;
        }

        li {
            margin: 15px 0;
        }

        /* 链接按钮样式 */
        a {
            text-decoration: none;
            padding: 12px 25px;
            background-color: #607d8b; /* 莫兰迪蓝绿色 */
            color: white;
            font-size: 18px;
            border-radius: 10px;
            display: inline-block;
            transition: background-color 0.3s ease, transform 0.2s ease;
        }

        a:hover {
            background-color: #78909c;
            transform: translateY(-2px);
        }

        a:active {
            background-color: #546e7a;
            transform: translateY(0);
        }

        /* 响应式设计 */
        @media (max-width: 480px) {
            .container {
                padding: 20px;
                width: 90%;
            }

            h3 {
                font-size: 24px;
            }

            a {
                font-size: 16px;
                padding: 10px 20px;
            }
        }
    </style>
</head>
<body>

<div class="container">
    <h3>${username} 欢迎你的探索!</h3>
    <ul>
        <li><a href="index/login">登录</a></li>
        <li><a href="index/register">获取ROY6账号</a></li>
    </ul>
</div>

</body>
</html>

 3、注册页面 register.jsp页面

由index.jsp页面点击注册按钮跳转而来,主要实现注册功能。这里连接数据库,判断用户的合法性。注册成功跳转success.jsp页面,失败跳转到error.jsp页面,已经注册过则继续停留在此页面,提示“注册失败,用户名已存在“。

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            margin: 0;
            padding: 0;
            background: url('https://wx4.sinaimg.cn/mw690/007btvx7ly1h7zumoa5l8j30u01mtgru.jpg') no-repeat center center fixed;
            background-size: cover;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            overflow: hidden;
            animation: backgroundAnimation 10s infinite alternate;
        }

        .container {
            width: 100%;
            max-width: 400px;
            background-color: #ffffff;
            padding: 30px;
            border-radius: 12px;
            box-shadow: 0 8px 20px rgba(0, 0, 0, 0.1);
            animation: fadeIn 0.8s ease-in-out;
        }

        @keyframes fadeIn {
            from {
                opacity: 0;
                transform: translateY(-20px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        h2 {
            font-size: 28px;
            color: #7a7975; /* 莫兰迪灰色 */
            margin-bottom: 20px;
            text-align: center;
            font-family: 'Comic Sans MS', cursive;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-group label {
            font-size: 16px;
            color: #8A9A8E; /* 深莫兰迪灰褐色 */
            margin-bottom: 5px;
            display: block;
            text-align: left;
        }

        .form-group input {
            width: 100%;
            padding: 12px;
            font-size: 16px;
            border: 1px solid #ddd;
            border-radius: 8px;
            box-sizing: border-box;
            outline: none;
            transition: all 0.3s ease;
        }

        .form-group input:focus {
            border-color: #a6a398; /* 莫兰迪绿灰色 */
            box-shadow: 0 0 10px rgba(166, 163, 152, 0.3);
        }

        .form-group p {
            color: #c94e50; /* 柔和的莫兰迪红色 */
            font-size: 14px;
        }

        .button-container {
            display: flex;
            gap: 10px;
            justify-content: space-between;
        }

        .button-container button {
            width: 48%;
            padding: 12px;
            font-size: 16px;
            border-radius: 8px;
            border: none;
            cursor: pointer;
            transition: all 0.3s ease;
        }

        .reset-button {
            background-color: #d7d3cc; /* 莫兰迪浅灰色 */
            color: #4f4d49; /* 深莫兰迪灰 */
        }

        .reset-button:hover {
            background-color: #c8c4bb; /* 更深的莫兰迪灰 */
        }

        .submit-button {
            background-color: #A8B3A5; /* 莫兰迪绿灰色 */
            color: #ffffff;
        }

        .submit-button:hover {
            background-color: #8f8c80; /* 更深的莫兰迪绿灰色 */
        }

        @media (max-width: 480px) {
            .container {
                padding: 20px;
            }

            h2 {
                font-size: 24px;
            }

            .form-group label {
                font-size: 14px;
            }

            .form-group input {
                font-size: 14px;
                padding: 10px;
            }

            .button-container button {
                font-size: 14px;
                padding: 10px;
            }
        }
    </style>
</head>
<body>
<div class="container">
    <h2>注册</h2>
    <%
        String error = (String) request.getAttribute("error");
        if (error != null) {
    %>
    <div class="form-group">
        <p><%= error %></p>
    </div>
    <%
        }
    %>
    <form action="${pageContext.request.contextPath}/doRegister" method="post">
        <div class="form-group">
            <label for="registerUsername">用户名</label>
            <input type="text" id="registerUsername" name="username" required>
        </div>
        <div class="form-group">
            <label for="password">密码</label>
            <input type="password" id="password" name="password" required>
        </div>
        <div class="form-group">
            <label for="confirmPassword">确认密码</label>
            <input type="password" id="confirmPassword" name="confirmPassword" required>
        </div>
        <div class="button-container">
            <button type="reset" class="reset-button">重置</button>
            <button type="submit" class="submit-button">注册</button>
        </div>
    </form>
</div>
</body>
</html>

 4、注册成功success.jsp页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册成功</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            margin: 0;
            padding: 0;
            background: url('https://wx2.sinaimg.cn/mw690/006Gi90nly1h6p18g6sipj31h40u0791.jpg') no-repeat center center fixed;
            background-size: cover;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            overflow: hidden;
            animation: backgroundAnimation 10s infinite alternate;
        }

        .container {
            background-color: white;
            padding: 40px;
            background-color: rgba(255, 255, 255, 0.95); /* 背景半透明 */
            border-radius: 16px;
            box-shadow: 0 6px 15px rgba(0, 0, 0, 0.1); /* 阴影效果 */
            width: 100%;
            max-width: 420px;
            text-align: center;
        }

        h1 {
            font-family: 'Comic Sans MS', cursive, sans-serif; /* 可爱的卡通字体 */
            font-size: 36px;
            color: #c58e72;
            margin-bottom: 20px;
            font-weight: 600;
        }

        p {
            font-size: 18px;
            color: #333;
            margin-bottom: 30px;
        }

        a {
            display: inline-block;
            font-size: 18px;
            color: white;
            background-color: #607d8b; /* Morandi blue */
            padding: 12px 24px;
            text-decoration: none;
            border-radius: 10px;
            transition: background-color 0.3s ease, transform 0.2s ease;
        }

        a:hover {
            background-color: #78909c;
            transform: translateY(-2px);
        }

        a:active {
            background-color: #546e7a;
            transform: translateY(1px);
        }
    </style>
</head>
<body>

<div class="container">
    <h1>注册成功</h1>
    <p>欢迎,您已成为ROY6新成员!</p>
    <a href="${pageContext.request.contextPath}/login">前往探索ROY6商城</a>
</div>

</body>
</html>

5、注册失败error.jsp页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册失败</title>
    <style>
        /* 设置背景色 */
        body {
            font-family: Arial, sans-serif;
            background-color: #f8d7da; /* 轻微红色背景,表示失败 */
            margin: 0;
            height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        .container {
            text-align: center;
            background-color: white;
            padding: 30px 50px;
            border-radius: 10px;
            box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
            width: 100%;
            max-width: 400px;
        }

        h1 {
            font-size: 24px;
            color: #721c24; /* 错误提示的红色 */
            margin-bottom: 20px;
        }

        p {
            font-size: 18px;
            color: #721c24;
            margin-bottom: 30px;
        }

        a {
            text-decoration: none;
            padding: 12px 20px;
            background-color: #4CAF50;
            color: white;
            font-size: 16px;
            font-weight: bold;
            border-radius: 6px;
            display: inline-block;
            transition: background-color 0.3s ease, transform 0.2s ease;
        }

        a:hover {
            background-color: #45a049;
            transform: translateY(-2px);
        }

        a:active {
            transform: translateY(0);
        }
    </style>
</head>
<body>

<div class="container">
    <h1>注册失败</h1>
    <p>抱歉,您的注册未能成功。请重试</p>
    <a href="${pageContext.request.contextPath}/register">返回注册页面</a>
</div>
</body>
</html>

6、登录login.jsp页面

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录</title>
    <style>

        /* 页面基础样式 */
        body {
            font-family: 'Arial', sans-serif;
            margin: 0;
            padding: 0;
            background: url('https://wx4.sinaimg.cn/mw690/007btvx7ly1h7zumt0ty2j31hc0u0wkx.jpg') no-repeat center center fixed;
            background-size: cover;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            overflow: hidden;
            animation: backgroundAnimation 10s infinite alternate;
        }

        .container {
            width: 100%;
            max-width: 400px;
            padding: 40px;
            background-color: rgba(255, 255, 255, 0.95); /* 背景半透明 */
            border-radius: 12px;
            box-shadow: 0 6px 15px rgba(0, 0, 0, 0.1); /* 阴影效果 */
            text-align: center;
            animation: fadeIn 1s ease-in-out;
        }

        /* 标题样式:使用莫兰迪色系 */
        h2 {
            font-size: 30px;
            color: #A8B3A5; /* 莫兰迪绿 */
            font-weight: 600;
            margin-bottom: 30px;
            letter-spacing: 1px;
            font-family: 'Georgia', serif;
        }

        /* 表单样式 */
        form {
            display: flex;
            flex-direction: column;
            gap: 20px;
        }

        .form-group {
            display: flex;
            flex-direction: column;
            align-items: flex-start;
        }

        .form-group label {
            margin-bottom: 8px;
            font-weight: bold;
            color: #6D7D6E; /* 莫兰迪灰绿 */
        }

        .form-group input {
            width: 100%;
            padding: 12px;
            border: 1px solid #ddd;
            border-radius: 8px;
            font-size: 16px;
            transition: border-color 0.3s ease, box-shadow 0.3s ease;
        }

        /* 输入框聚焦效果 */
        .form-group input:focus {
            border-color: #A8B3A5; /* 莫兰迪绿 */
            outline: none;
            box-shadow: 0 0 8px rgba(169, 179, 165, 0.6);
        }

        /* 按钮样式 */
        .form-group button {
            padding: 12px 20px;
            border: none;
            border-radius: 6px;
            background-color: #A8B3A5; /* 莫兰迪绿 */
            color: white;
            font-size: 16px;
            font-weight: bold;
            cursor: pointer;
            transition: background-color 0.3s ease, transform 0.2s ease;
        }

        .form-group button:hover {
            background-color: #8A9A8E; /* 更深的莫兰迪绿 */
            transform: translateY(-3px);
        }

        .form-group button:active {
            transform: translateY(0);
        }

        /* 提交按钮 */
        .submit-button {
            background-color: #A8B3A5;
        }

        .submit-button:hover {
            background-color: #8A9A8E;
        }

        /* 动画效果 */
        @keyframes fadeIn {
            from {
                opacity: 0;
                transform: translateY(-20px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        /* 响应式设计 */
        @media (max-width: 480px) {
            .container {
                padding: 20px;
            }

            .form-group input {
                padding: 10px;
            }

            .form-group button {
                padding: 12px 16px;
            }

            h2 {
                font-size: 26px;
            }
        }
    </style>
</head>
<body>

<div class="container">
    <h2>登录</h2>
    <form action="${pageContext.request.contextPath}/doLogin" method="post">
        <div class="form-group">
            <label for="username">用户名</label>
            <input type="text" id="username" name="username" required>
        </div>
        <div class="form-group">
            <label for="password">密码</label>
            <input type="password" id="password" name="password" required>
        </div>
        <div class="form-group">
            <button type="submit" class="submit-button">登录</button>
        </div>
    </form>
</div>

</body>
</html>

7、欢迎welcome.jsp页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>欢迎来到ROY6商城</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            margin: 0;
            padding: 0;
            background: url('https://wx4.sinaimg.cn/mw690/007doFNOgy1hf8lzljzodj323u16qqv5.jpg') no-repeat center center fixed;
            background-size: cover;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            overflow: hidden;
        }

        .container {
            text-align: center;
            background-color: rgba(255, 255, 255, 0.9);
            padding: 30px 40px;
            border-radius: 15px;
            box-shadow: 0 8px 20px rgba(0, 0, 0, 0.2);
            width: 100%;
            max-width: 500px;
            animation: fadeIn 1s ease-in-out;
            position: relative;
            z-index: 1;
        }

        h1 {
            font-size: 30px;
            color: #444;
            margin-bottom: 20px;
            font-family: 'Comic Sans MS', cursive;
        }

        p {
            font-size: 18px;
            color: #666;
            margin-bottom: 20px;
        }

        a {
            display: inline-block;
            text-decoration: none;
            padding: 12px 25px;
            margin: 10px 0;
            background-color: #A8B3A5; /* 修改为莫兰迪绿色 */
            color: white;
            font-size: 16px;
            border-radius: 8px;
            transition: background-color 0.3s ease, transform 0.2s ease;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
        }

        a:hover {
            background-color: #8A9A8E; /* 悬停时更深的莫兰迪绿色 */
            transform: translateY(-3px);
        }

        a:active {
            transform: translateY(0);
        }

        @keyframes fadeIn {
            from {
                opacity: 0;
                transform: translateY(-20px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        /* 新增样式:绝对定位文本 */
        .text-overlay {
            position: absolute;
            top: 10%;
            left: 50%;
            transform: translateX(-50%);
            background-color: rgba(255, 255, 255, 0.7);
            padding: 20px;
            border-radius: 15px;
            text-align: center;
            font-size: 17px;
            color: #444;
            width: 80%;
            max-width: 600px;
            box-shadow: 0 8px 15px rgba(0, 0, 0, 0.3);
            animation: fadeIn 1s ease-in-out;
        }

        @media (max-width: 480px) {
            .container {
                padding: 20px;
                width: 90%;
            }

            h1 {
                font-size: 24px;
            }

            p {
                font-size: 16px;
            }

            a {
                font-size: 14px;
            }

            .text-overlay {
                font-size: 16px;
                width: 90%;
            }
        }
    </style>
</head>
<body>
<!-- 新增的文字容器 -->
<div class="text-overlay">
    ROY6住在一个未知的BAOBAO森林里,<br>
    它们喜欢音乐、舞蹈,还喜欢用自己被赋予的神秘力量,帮助地球上的人们。<br>
    如果你忽然感到幸福,不再孤单,那是因为ROY6来到了你身边!<br>
    在日常生活中,当你想要获得小小的幸运时,就请呼叫ROY6吧!
</div>

<div class="container">
    <h1>${username} 欢迎来到ROY6!</h1>
    <p>请选择您想进行的操作:</p>
    <a href="${pageContext.request.contextPath}/add">新增Roy6商品</a>
    <a href="${pageContext.request.contextPath}/list">查看Roy6商品</a>
</div>
</body>
</html>

8、相关配置

(1)web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

(2)lib包

这里因为我用的是 Tomcat10.0.23 的版本,所以使用了6.0的 jar 包,大家使用对应自己的就行,或者采用Maven配置

(3)spring配置(applicationContext.xml)

创建配置文件applicationContext.xml,在该文件中配置id为dataSource的数据源Bean和id为jdbcTemplate的JDBC模板Bean,并将数据源注入到JDBC模板中。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

    <!-- Enable Spring MVC components and default configuration -->
    <mvc:annotation-driven />

    <!-- Scan for components (controllers, services, repositories) in the specified package -->
    <context:component-scan base-package="com.example.demo" />
    <context:component-scan base-package="com.example.demo.controller"/>

    <!-- 视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
        <!-- 前缀 -->
        <property name="prefix" value="/WEB-INF/views/" />
        <!-- 后缀 -->
        <property name="suffix" value=".jsp" />
    </bean>

    <!-- DataSource configuration for connecting to MySQL database -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/spring_db?useUnicode=true characterEncoding=UTF-8 useSSL=false serverTimezone=UTC" />
        <property name="username" value="root" />
        <property name="password" value="这里写你的数据库密码" />
    </bean>

    <!-- JdbcTemplate bean for database operations -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean id="txManage" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
            <property name="transactionManager" ref="txManage"/>
    </bean>

    <bean id="resourceHttpRequestHandler" class="org.springframework.web.servlet.resource.ResourceHttpRequestHandler">
        <property name="locations">
            <list>
                <value>classpath:/static/</value>
            </list>
        </property>
    </bean>
</beans>

9、后端代码

(1)编写Controller层

创建IndexController类,用于实现首页跳转登录注册操作。

IndexController.java
package com.example.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/index")
public class IndexController {
    @RequestMapping(value = "/login",method = RequestMethod.GET)
    public String login(){
        return "login";
    }
    @RequestMapping("/register")
    public String register(){
        return "register";
    }
}

创建IndexController类,用于实现用户登录注册操作。

UserController.java
package com.example.demo.controller;

import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;


@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/login")

    public String showLoginForm() {
        return "login";
    }

    @PostMapping("/doLogin")
    public ModelAndView login(@RequestParam("username") String username, @RequestParam("password") String password){
        ModelAndView modelAndView = new ModelAndView();
        String result = userService.Login(username,password);
        if("success".equals(result)){
            modelAndView.addObject("username",username);
            modelAndView.setViewName("welcome"); //设置视图为welcome.jsp
        }else{
            modelAndView.addObject("error",true);
            modelAndView.setViewName("login");

        }
        return modelAndView;
    }


    @GetMapping("/register")
    public String showRegisterForm(){
        return "register";
    }

    @PostMapping("/doRegister")
    public ModelAndView Register(@RequestParam("username") String username, @RequestParam("password") String password, @RequestParam("confirmPassword") String confirmPassword) {
        ModelAndView modelAndView = new ModelAndView();
        if (!confirmPassword.equals(password)) {
            modelAndView.addObject("error", "两次密码不一致");
            modelAndView.setViewName("register");  // 返回注册页面
            return modelAndView;
        }
        try {
            userService.Register(username, password);
            modelAndView.setViewName("success"); // 注册成功后跳转到success.jsp页面
            modelAndView.addObject("username", username);
        } catch (IllegalArgumentException e) {
            modelAndView.addObject("error", "注册失败,用户名已存在");
            modelAndView.setViewName("register"); // 注册失败,仍然在注册页面
        }
        return modelAndView;
    }

}

(2)编写Dao层方法

创建UsersDAO接口,在UsersDAO接口中声明查询所有用户信息和更新的方法。

UsersDAO.java
package com.example.demo.dao;

import com.example.demo.model.Users;

import java.util.List;

public interface UsersDAO {
    public int update(String sql, Object[] params);
    public List<Users> query(String sql, Object[] params);
}

(3)实现Dao层方法

创建UsersDAOImpl实现类,在UsersDAOImpl类中实现UsersDAO接口中的 update() 和 query() 方法。

UsersDAOImpl.java
package com.example.demo.dao;

import com.example.demo.model.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class UsersDAOImpl implements UsersDAO {  // 加上 implements UsersDAO

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public int update(String sql, Object[] params) {
        return jdbcTemplate.update(sql, params);
    }

    @Override
    public List<Users> query(String sql, Object[] params) {
        RowMapper<Users> rowMapper = new BeanPropertyRowMapper<>(Users.class);
        return jdbcTemplate.query(sql, rowMapper, params);
    }
}

(4)编写实体类

创建Users类,在该类中定义username、password属性,以及属性对应的getter/setter方法。

Users.java
package com.example.demo.model;

public class Users {
    private String username;
    private String password;

    public String getName() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

(5)编写服务层接口

创建UserService接口,在UserService接口中声明登录和注册的方法。

UserService.java
package com.example.demo.service;

public interface UserService {
    public String Login(String username, String password);

    public void Register(String username, String password);
}

(6)实现服务层方法

创建UserServiceImpl实现类,在UserServiceImpl类中实现UserService接口中的 Login() 和 Register() 方法。

UserServiceImpl.java
package com.example.demo.service;

import com.example.demo.dao.UsersDAO;
import com.example.demo.model.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UsersDAO usersDAO;

    @Override
    public String Login(String username, String password) {
        // 使用占位符避免SQL注入
        String sql = "SELECT * FROM user WHERE username = ? AND password = ?";
        List<Users> users = usersDAO.query(sql, new Object[]{username, password});
        if (!users.isEmpty()) {
            return "success";
        }
        return "login?error=true";
    }

    @Override
    public void Register(String username, String password) {
        String checkUserSql = "SELECT * FROM user WHERE username = ?";
        List<Users> existingUsers = usersDAO.query(checkUserSql, new Object[]{username});
        if (!existingUsers.isEmpty()) {
            throw new IllegalArgumentException("Username already exists");
        }
        String insertUserSql = "INSERT INTO user (username, password) VALUES (?, ?)";
        usersDAO.update(insertUserSql, new Object[]{username, password});
    }
}

10、 查看运行结果

运行结果当然在上边就展示了哈哈哈,会遇到同样喜欢Roy6家族的友友嘛~嘻嘻~

哈哈,到这里就结束了~

码字不易,别忘记 点赞收藏哦!


http://www.kler.cn/a/408020.html

相关文章:

  • 二叉树:堆的建立和应用
  • html+js实现图片的放大缩小等比缩放翻转,自动播放切换,顺逆时针旋转
  • 排序算法(选择排序、直接插入排序、冒泡排序、二路归并排序)(C语言版)
  • 服务机器人三甲坎德拉:用智能化开启售后服务新篇章
  • 在CentOS 7上配置Nginx的TCP端口转发
  • AWTK fscript 中的 大端小端扩展函数
  • go语言示例代码
  • 微信小程序包之加农炮游戏
  • Spring Aop+自定义注解实践(待完善日志)
  • windows10下3DGS环境配置
  • Rust 力扣 - 2466. 统计构造好字符串的方案数
  • Redis的理解
  • cangjie (仓颉) vscode环境搭建
  • 2024年亚太杯数学建模竞赛赛题选题人数发布
  • Qt之QWidget相关
  • 7、深入剖析PyTorch nn.Module源码
  • 【入门篇】欧几里德最差序列——多语言求解版
  • 后端:事务
  • RabbitMQ2:介绍、安装、快速入门、数据隔离
  • 八、无刷电机电压电流温度采集
  • CSS布局学习1
  • Oracle SQL优化②——访问路径
  • 使用 Elastic AI Assistant for Search 和 Azure OpenAI 实现从 0 到 60 的转变
  • 2-测试bigcache做进程内缓存 --开源项目obtain_data测试
  • Python爬虫:获取1688店铺详情的实战指南
  • JMeter监听器与压测监控之 InfluxDB