如何检查EL中的请求是否存在会话?我正在尝试类似的东西:
<c:if test="${pageContext.request.session != null}"> ... </c:if>
但似乎永远不会为空。
确实从来没有null
。该会话始终存在于JSP EL中,除非您添加
<%@page session="false" %>
到JSP的顶部。然后,您可以按照以下方式检查会话(仅适用于EL 2.2!):
<c:if test="${pageContext.request.getSession(false) != null}">
<p>The session has been created before.</p>
</c:if>
我不确定具体的功能要求是什么。如果您要检查会话是新建的还是已经创建的,请HttpSession#isNew()
改用。
<c:if test="${not pageContext.session['new']}">
<p>You've already visited this site before.</p>
</c:if>
<c:if test="${pageContext.session['new']}">
<p>You've just started the session with this request!</p>
</c:if>
(括号表示法new
是强制性的,因为它new
是Java语言中的保留文字)
如果您依赖特定的会话属性,例如设置为的登录用户
session.setAttribute("user", user);
那么您应该宁愿拦截它:
<c:if test="${not empty user}">
<p>You're still logged in.</p>
</c:if>
<c:if test="${empty user}">
<p>You're not logged in!</p>
</c:if>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句