1. 程式人生 > >SSM框架實現登陸註冊功能-從零學習SSM框架(4)

SSM框架實現登陸註冊功能-從零學習SSM框架(4)

前言

很久沒有做過後臺開發了,最近朝花夕拾,重新回憶回憶SSM框架的基本知識。記記筆記。

正文

本文將從零開始實現網路中常用的登陸註冊功能。所有操作均基於前幾篇關於SSM框架文章操作。

準備

依然是mysql資料庫,資料庫與表名和之前的一致,我這幾篇文章是一個專案中的,哦哈哈哈哈,改一下欄位就好,如下:

原理再現

  • 註冊
    所謂註冊即是向系統中新增資料,新增一條登陸使用者名稱,密碼等等。
  • 登陸
    登陸是將使用者的輸入資料與資料庫中的原有資料進行對比,輸入正確即對比成功則登陸成功進行相關操作,否則檢測錯誤存在。

程式碼實現

註冊

註冊頁面index.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Index</title>
    <script src="/js/jquery-3.2.1.min.js" type="text/javascript"></script>
</head>
<body>
<br>
<br>
<form action="/registe" method="post">
    <table>
        <tr>
            <td>
                使用者名稱:
            </td>
            <td>
                <input type="text" name="name" placeholder="name">
            </td>
        </tr>
        <tr>
            <td>
                密碼:
            </td>
            <td>
                <input type="password" name="password">
            </td>
        </tr>
        <tr>
            <td>
                &nbsp;
            </td>
            <td>
                <input type="submit" value="提交">
            </td>
        </tr>
    </table>
</form>
</body>
<script type="text/javascript">
    $(function () {
        if ((${result})) {
            if ((${result}) == 1) {
                alert("註冊成功!");
            } else {
                alert("未知錯誤,重新註冊");
            }
        } else {
            alert("");
        }
    })
</script>
</html>

對應url/registe的controller對映函式

package com.springmvc.controller;


import com.springmvc.entity.User;
import com.springmvc.services.UserServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.UUID;


@Controller
public class IndexController {

    @Autowired
    private UserServices userServices;

    @RequestMapping(value = "/registe",method = RequestMethod.POST)
    public String toregiste(@RequestParam String name, @RequestParam  String password, Model model) {
        User user = new User();
        String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase();
        user.setUuid(uuid);
        user.setName(name.trim());
        user.setPassword(password.trim());
        try {
            userServices.insert(user);
            model.addAttribute("result", "1");
        } catch (Exception e) {
            model.addAttribute("result", "0");
        }
        return "index";

    }
}

執行一遍,即可以向資料庫中插入使用者資料了

後臺資料庫資料存在了
在這裡插入圖片描述

登陸

新建login.jsp

<%--
  Created by IntelliJ IDEA.
  User: Nick
  Date: 2018/10/25
  Time: 10:05
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Login</title>
    <script type="text/javascript" src="/js/jquery-3.2.1.min.js"></script>
</head>
<body>
<br>
<br>
<form action="/login" method="post">
    <table>
        <tr>
            <td>
                使用者名稱:
            </td>
            <td>
                <input type="text" name="name" placeholder="name">
            </td>
        </tr>
        <tr>
            <td>
                密碼:
            </td>
            <td>
                <input type="password" name="password">
            </td>
        </tr>
        <tr>
            <td>
                &nbsp;
            </td>
            <td>
                <input type="submit" value="登陸">
            </td>
        </tr>
    </table>
</form>
</body>
<script type="text/javascript">
    $(function () {
        if ((${result}) != null) {
            if ((${result}) == "1") {
            } else if ((${result}) == "0") {
                alert("賬號或密碼不正確");
            } else if ((${result}) == "-1") {
                alert("請填寫完整");
            } else {
                alert("c");
            }
        }
    })
</script>
</html>

以及對應的controller

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String toLogin(@RequestParam String name, @RequestParam  String password, Model model, HttpSession session) {
        if (name.equals("") || password.equals("")) {
            model.addAttribute("result", "-1");

        } else {
            User user = userServices.selectByName(name.trim());
            if (user !=null) {
                if (password.equals(user.getPassword())) {
                    session.setAttribute("user", user);
                    model.addAttribute("user",user);
                    model.addAttribute("result", "1");
                    return "user";
                }
            } else {
                model.addAttribute("result", "0");

            }
        }
        return "login";
    }

登陸成功後的user.jsp

<%--
  Created by IntelliJ IDEA.
  User: Nick
  Date: 2018/10/25
  Time: 10:44
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>User</title>
    <script type="text/javascript" src="/js/jquery-3.2.1.min.js"></script>
</head>
<body>
你好,歡迎你${user.name},<a href="/logout">退出登陸</a>
</body>
</html>

退出登陸即銷燬session,

@RequestMapping(value = "/logout")
    public String toLogout(HttpSession session) {
        session.removeAttribute("user");
        return "login";
    }

註冊登陸退出的整體流程就是這樣了