0
点赞
收藏
分享

微信扫一扫

SpringBoot+thymeleaf,在html页面获取session

瑾谋 2022-12-14 阅读 31


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>

 

举报

相关推荐

0 条评论