Spring Boot项目实践-员工管理系统(四)·登录功能实现

本次实战项目主要是借鉴b站上的视频资源【狂神说Java】SpringBoot最新教程IDEA版通俗易懂完成的,有需求的话,可以直接去b站观看完整的视频教程,本文若有不对之处,望不吝赐教,谢谢~
博文前提:

一、新建登录控制器

LoginController.java

package com.example.employee_management.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @className: LoginController
 * @description: 登录控制器
 */
@Controller
public class LoginController {

    /**
     * 登录
     * @param username
     * @param password
     * @param model
     * @return
     */
    @RequestMapping("/user/login")
    public String login(@RequestParam("username")String username,
                        @RequestParam("password")String password, Model model){

        //用户名不为空且密码正确  注意这里的数据是默认密码为123456,便于调试
        if(!StringUtils.isEmpty(username) && "123456".equals(password)){
            return "redirect:/main.html";
        }else {
            //用户名或密码错误
            model.addAttribute("msg","用户名或密码错误");
            return "index";
        }
    }
}

二、修改index.html文件

(1)修改表单的action为@{/user/login}
(2)添加提示信息
(3)对于用户名和密码输入框添加name属性

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">
    <title>Signin Template for Bootstrap</title>
    <!-- Bootstrap core CSS -->
    <link th:href="@{/css/bootstrap.min.css}" rel="stylesheet">
    <!-- Custom styles for this template -->
    <link th:href="@{/css/signin.css}" rel="stylesheet">
</head>

<body class="text-center">
<!--修改表单的action为@{/user/login}-->
<form class="form-signin" th:action="@{/user/login}">
    <img class="mb-4" th:src="@{/img/bootstrap-solid.svg}" alt="" width="72" height="72" src="">
    <!--修改-->
    <h1 class="h3 mb-3 font-weight-normal" th:text="#{login.tip}">Please sign in</h1>

    <!--添加提示信息-->
    <!--如果msg信息不为空,则显示消息-->
    <p style="color:red" th:text="${msg}" th:if="${not #strings.isEmpty(msg)}"></p>

    <!--修改-->
    <!--<label class="sr-only" th:text="#{login.username}">Username</label>-->
    <label>
        <!--修改-->
        <input type="text" name="username" class="form-control" th:placeholder="#{login.username}" required="" autofocus="">
    </label>
    <!--修改-->
    <!--<label class="sr-only" th:text="#{login.password}">Password</label>-->
    <label>
        <!--修改-->
        <input type="password" name="password" class="form-control" th:placeholder="#{login.password}" required="">
    </label>
    <div class="checkbox mb-3">
        <!--修改-->
        <label>
            <input type="checkbox" value="remember-me">[[#{login.rememberMe}]]
        </label>
    </div>
    <!--修改-->
    <button class="btn btn-lg btn-primary btn-block" type="submit">[[#{login.signInBtn}]]</button>
    <p class="mt-5 mb-3 text-muted">© 2017-2018</p>
    <!--修改-->
    <a class="btn btn-sm" th:href="@{/index.html(language='zh_CN')}">中文</a>
    <a class="btn btn-sm" th:href="@{/index.html(language='en_US')}">English</a>
</form>

</body>

</html>
三、添加用户主页

在MyMvcConfig类中添加用户主页页面控制器

package com.example.employee_management.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @className: MyMvcConfig
 * @description: MVC 控制器  借助注解完成控制器而不用手动编写
 */
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {

    /**
     * MVC 添加首页控制器
     *
     * @param registry
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //这里 “/”和“/index.html”效果一样,因为web项目默认页是index.html
        registry.addViewController("/").setViewName("index");
        registry.addViewController("/index.html").setViewName("index");

        //添加用户主页
        registry.addViewController("/main.html").setViewName("dashboard");
    }


    /**
     * 将国际化组件放入ioc容器中
     *
     * @return
     */
    @Bean
    public LocaleResolver localeResolver() {
        return new MyLocaleResolver();
    }
}

启动项目。输入正确的用户名和密码后,会自动跳转到dashboard.html页面;当输入错误时会有提示信息
在这里插入图片描述

四、总结

实现登录功能需要注意的地方有:

  • 建立一个登录控制器类,且要在MVC配置类中作相应配置
  • 利用form表单提交信息
  • 目前实现的登录功能不完善,后续需要加上拦截器使其更安全

在这里插入图片描述
2020.04.11

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值