0
点赞
收藏
分享

微信扫一扫

ModelMap传递数据(简洁)

心存浪漫 2022-01-26 阅读 131
@RestController
@RequestMapping("/user")
public class UserController {
	
	@GetMapping("/login.do")
    public ModelAndView login(){
        System.out.println("显示登录界面");
        return new ModelAndView("login");
    }

    @PostMapping("handle_login")
    public ModelAndView handleLogin(String username,String password,
                                    ModelMap model){
        if ("robin".equals(username)){
            if ("123".equals(password)){
                model.put("message","欢迎回来");
                return new ModelAndView("message");
            }else {
                model.put("message","密码错误");
                return new ModelAndView("message");
            }
        }else {
            model.put("message","用户名错误");
            return new ModelAndView("message");
        }
    }
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://java.sun.com/jsf/composite">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>欢迎回来</h1>
    <p th:text="${message}"></p>
</body>
</html>
举报

相关推荐

0 条评论