登录页 首页开发思路

登录页 首页开发思路

  • 创建Sprintboot项目
    • 选择依赖
      • springboot boot dev tools 实时编译
      • lombok 简化编译
      • Spring Configuration Processor 自定义配置有提示
      • Spring-web web项目
      • Thymeleaf 页面渲染
      • SQL相关

登录页面 login

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <link href="../static/css/login.css" th:href="@{css/login.css}" rel="stylesheet"></link>
    <title>登陆页面</title>
</head>
<body>
<div class="main">
    <div class="title">
        <span>密码登录</span>
    </div>

    <div class="title-msg">
        <span>请输入登录账户和密码</span>
    </div>

    <form class="login-form" action="index.html" method="post" th:action="@{/login}" >
        <!--输入框-->
        <div class="input-content">
            <label style="color: red" th:text="${msg}"></label>
            <!--autoFocus-->
            <div>
                <input type="text" autocomplete="off"
                       placeholder="用户名" name="userName" required/>
            </div>

            <div style="margin-top: 16px">
                <input type="password"
                       autocomplete="off" placeholder="登录密码" name="password" required maxlength="32"/>
            </div>
        </div>

        <!--登入按钮-->
        <div style="text-align: center">
            <button type="submit" class="enter-btn" >登录</button>
        </div>

        <div class="foor">
            <div class="left"><span>忘记密码 ?</span></div>

            <div class="right"><span>注册账户</span></div>
        </div>
    </form>

</div>
</body>
</html>
body{
    background: #353f42;
}

*{
    padding: 0;
    margin: 0;
}

.main {
    margin: 0 auto;
    padding-left: 25px;
    padding-right: 25px;
    padding-top: 15px;
    width: 350px;
    height: 350px;
    background: #FFFFFF;
    /*以下css用于让登录表单垂直居中在界面,可删除*/
    position: absolute;
    top: 50%;
    left: 50%;
    margin-top: -175px;
    margin-left: -175px;
}

.title {
    width: 100%;
    height: 40px;
    line-height: 40px;
}

.title span {
    font-size: 18px;
    color: #353f42;
}

.title-msg {
    width: 100%;
    height: 64px;
    line-height: 64px;
}

.title:hover{
    cursor: default	;
}

.title-msg:hover{
    cursor: default	;
}

.title-msg span {
    font-size: 12px;
    color: #707472;
}

.input-content {
    width: 100%;
    height: 120px;
}

.input-content input {
    width: 330px;
    height: 40px;
    border: 1px solid #dad9d6;
    background: #ffffff;
    padding-left: 10px;
    padding-right: 10px;
}

.enter-btn {
    width: 350px;
    height: 40px;
    color: #fff;
    background: #0bc5de;
    line-height: 40px;
    text-align: center;
    border: 0px;
}

.foor{
    width: 100%;
    height: auto;
    color: #9b9c98;
    font-size: 12px;
    margin-top: 20px;
}

.enter-btn:hover {
    cursor:pointer;
    background: #1db5c9;
}

.foor div:hover {
    cursor:pointer;
    color: #484847;
    font-weight: 600;
}

.left{
    float: left;
}
.right{
    float: right;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>主页</title>
</head>
<body>
<h1> 主页</h1>


</body>
</html>
package com.admin.bean;

import lombok.Data;

@Data
public class User {
    private String userName;
    private String password;
}

package com.admin.controller;

import com.admin.bean.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;

@Controller
public class IndexController {

    @GetMapping({"/","/login"})
    public String loginPage(){
        return "login";
    }
    @PostMapping("/login")
    public String indexPage(User user, HttpSession session, Model model){
        if(user!=null&&user.getPassword()!=null&&user.getUserName()!=null&&"123456".equals(user.getPassword())){
            //把登录成功的用户保存起来
            session.setAttribute("loginUser",user);
            //登录成功重定向页面;防止表单重复提交
            return "redirect:/index.html";
        }else {
            model.addAttribute("msg","账号密码错误");
            return "login";
        }


    }
    @GetMapping("/index.html")
    public String mainPage(){


        return "index";
    }
}
posted @ 2022-03-15 16:49  Tian-Jotian-Yongxing  阅读(57)  评论(0)    收藏  举报