springboot 简单的登录(不带数据库)

2021/12/15 2:22:27

本文主要是介绍springboot 简单的登录(不带数据库),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

项目结构

 看一眼config包下的MyMvcConfig类      不用写也会 localhost:8080自动找index页面并跳转

//自己对mvc的一些配置,虽然springboot帮我们配置了,但是有些我想弄成别的样的所以得自己配一下
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //这样的话我在浏览器的地址栏打"/"或者"/index.html"都会跳转到index(首页) 虽然直接写localhost:8080也能自动跳转到首页
        registry.addViewController("/").setViewName("index");
        registry.addViewController("/index.html").setViewName("index");
    }

}

Controller

@Controller
public class LoginController {

//    @RequestMapping("/user/login")
//    public String login(User user){
//        if (user.username == "hi" || user.userpassword == "hi"){
//            return "dashboard";
//        }else {
//            model.addAttribute("msg","登陆失败");
//            return "index";
//        }
//    }

    @RequestMapping("/user/login")
    public String login(@RequestParam("username") String username, @RequestParam("userpassword") String userpassword, Model model){
        if (username == "hi" || userpassword == "hi"){
            return "dashboard";
        }else {
            model.addAttribute("msg","登陆失败");
            return "index";
        }
    }
}

前端页面

thymleaf的头

<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">

 



这篇关于springboot 简单的登录(不带数据库)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程