package com.baizhi.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class IndexController {
@GetMapping("index")
public String tologin (){
return "login";
}
}
package com.baizhi.controller;
import com.baizhi.entity.User;
import com.baizhi.sercive.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpSession;
@Controller
@RequestMapping("user")
public class UserController {
@Autowired
private UserService userService;
@PostMapping("login")
public String login(User user , HttpSession session){
User userDB=userService.login(user);
if(userDB!=null){
session.setAttribute("user",userDB);
return "redirect:/file/showAll";
}else{
return "redirect:/index";
}
}
}
```<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>用户登录</title>
</head>
<body>
<h1>欢迎访问用户文件管理系统</h1>
<form th: action="@{/user/login}" method="post">
username: <input type="text" name="username" /> <br>
password: <input type="text" name="password" /> <br>
<input type="submit" value="登录">
</form>
</body>
</html>
![图片说明](https://img-ask.csdn.net/upload/202010/04/1601765323_725462.png)![图片说明](https://img-ask.csdn.net/upload/202010/04/1601765332_61080.png)![图片说明](https://img-ask.csdn.net/upload/202010/04/1601765337_610847.png)