thymeleaf抛出项目上下文ServletContext ,session,request等信息

    @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

thymeleaf抛出项目上下文ServletContext ,session,request等信息

 

 

 

 

 

上一篇:javaweb之JSP+Servlet


下一篇:javascript-为什么parentNode替换td的所有元素而不是指定的元素?