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

[项目实战(一)]
[项目实战(二)]
[项目实践(三)]
[项目实践(五)]
[项目实践(六)]
[项目实践(七)]
[项目实践(八)]
[项目实践(九)]
一、新建登录控制器

LoginController.java

package com.xuyuan.springboot03web.controller;

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

import org.thymeleaf.util.StringUtils;

@Controller
public class LoginController {
    @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">
		<form class="form-signin" th:action="@{/user/login}">
			<img class="mb-4" th:src="@{/img/bootstrap-solid.svg}" alt="" width="72" height="72">
			<h1 class="h3 mb-3 font-weight-normal"th:text="#{login.tip}">Please sign in</h1>
			<p style="color:#ff0000" th:text="${msg}" th:if="${not #strings.isEmpty(msg)}"></p>
			<input type="text"name="username" class="form-control" th:placeholder="#{login.username}" required="" autofocus="">
			<input type="password" name="password" class="form-control" th:placeholder="#{login.password}" required="">
			<div class="checkbox mb-3">
				<label>
          <input type="checkbox" value="remember-me"> [[#{login.Remember}]]
				</label>
			</div>
			<button class="btn btn-lg btn-primary btn-block" type="submit">
				[[#{login.btn}]]
			</button>
			<p class="mt-5 mb-3 text-muted">© 2020-2021</p>
			<a class="btn btn-sm"th:href="@{/index.html(l='zn_CN')}">中文</a>
			<a class="btn btn-sm"th:href= "@{/index.html(l='en_US')}">English</a>
		</form>

	</body>

</html>

三、添加用户主页

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

package com.xuyuan.springboot03web.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;

@Configuration
public class MyMvcConfig implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");
        registry.addViewController("/index.html").setViewName("index");
        registry.addViewController("/main.html").setViewName("dashboard.html");
    }

    @Bean
    public LocaleResolver localeResolver(){
        return new MyLocaleResolver();
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值