@RequestMapping("/alls") public String allsinfo(HttpSession session, HttpServletRequest request,Model model){ session.setAttribute("username","小张session"); request.setAttribute("userpwd","666req"); request.getServletContext().setAttribute("usersex","男ctx"); String paths = request.getServletContext().getContextPath(); model.addAttribute("paths",paths); return "allsinfo"; }
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div>请求上下文内容</div> <div th:text="${#httpServletRequest.getAttribute('userpwd')}"></div> <div th:text="${userpwd}"></div> <div th:text="${session.username}"></div> <div th:text="${#servletContext.getAttribute('usersex')}"></div> <div th:text="${application.usersex}"></div> </body> </html>
这里需要注意的是有两种request取法,两种servletcontext取法,一种session