一:thymeleaf中使用session
1.基础对象访问(#session,需要contoller/action(HttpSession session))
<!-- 基础对象,等同于#httpSession(javax.servlet.http.HttpSession)-- >
<span th:text="${#session.getAttribute('')}">
<span th:text="${#session.getId()}">
2.web环境对象(#httpSession,需要contoller/action(HttpSession session)
javax.servlet.http.HttpSession实例)
--><!-- web环境对象(javax.servlet.http.HttpSession实例)-- >
<span th:text="${#httpSession.getAttribute('')}">
<span th:text="${#httpSession.getId()}">
3.web环境属性(session)
<!-- web环境属性(session属性) -->
<span th:text="${session.size()}">
<span th:text="${session.id}">
<!--<span th:text="${session.getId()}"></span> 错误的 -->
<span th:text="${session.isEmpty()}">
<span th:text="${session.keyname}">
二:sessio使用
1.Action设置:session.setAttribute("sessionBk", new LogonUser("Bk001", "BkCaff", "Bk000"));
2.Action获取:
1)代码直接获取
LogonUser logonUser1=(LogonUser) session.getAttribute("sessionBk");
2)action参数利用@SessionAttribute进行获取
public ModelAndView showList(@SessionAttribute("sessionBk") LogonUser logonUser){
System.out.println(logonUser.getUserId());
}
3)class's @SessionAttributes("sessionKey")+action参数@ModelAttribute("sessionKey")获取
@SessionAttributes("sessionBk")//类上
public class GirlController {
//action参数中
public ModelAndView showList(@ModelAttribute("sessionBk") LogonUser logonUser2){
System.out.println(logonUser2.getUserId());
}
}
3.thymeleaf获取:
以web环境session属性为例
<span th:text="${session.sessionBk.userId}">
网友评论