目录
1、编写index.html里面的登录表单的提交地址
2、编写LoginController
package com.zhou.controller;
import org.springframework.boot.web.servlet.server.Session;
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;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpSession;
@Controller
public class LoginController {
@RequestMapping("/user/login")
public String login(@RequestParam("username") String username, @RequestParam("password")String password, Model model, HttpSession session){
// 具体的业务
if (!StringUtils.isEmpty(username) && "123456".equals(password)){
session.setAttribute("loginUser", username); // 将登录者的信息存放在session里面,用作后面的拦截器权限问题
return "redirect:/main.html";
}else {
// 告诉用户登录错误
model.addAttribute("msg", "用户名或密码错误");
return "index";
}
}
}
使用重定向main.html效果对比
3、添加提示信息
4、最终执行效果
失败
成功