SpringBoot+thymeleaf,在html页面获取session

版权声明: https://blog.csdn.net/weixin_40550726/article/details/82626886

1.Controller层代码

@RequestMapping("userLogin")
	public String userLogin(@RequestParam("userName")String userName,@RequestParam("password")String password,HttpServletRequest request) {
		int n=userService.userLogin(userName, password);
		if(n==1) {
			HttpSession session=request.getSession();//获取session并将userName存入session对象
			session.setAttribute("userName", userName);
			return "user/index";
		}
		
		return "index";
	}

2.View层

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
登陆成功
<div th:text="${session.userName}"></div> <!--获取session中的userName -->
</body>
</html>

猜你喜欢

转载自blog.csdn.net/weixin_40550726/article/details/82626886