开发者

Get session value in struts/sitemesh

I'm using sitemesh with struts2. I want to create a login/logout link on the main layout pa开发者_StackOverflow社区ge. How do we get the value of session there.


Simple example

decorators.xml

<decorators defaultdir="/decorators">
    <decorator name="default" page="default.jsp">
        <pattern>/*</pattern>
    </decorator>
</decorators>

/decorators/default.jsp

With Struts2 Tag

<s:set var="isNoLogin">${empty pageContext.request.remoteUser}</s:set>

<s:if test="#isNoLogin">
    <s:a action="login">Login</s:a>
</s:if>
<s:else>
    <s:a action="logout">Logout</s:a>
</s:else>

or

<s:set var="remoteUser">${pageContext.request.remoteUser}</s:set>

<s:if test="#remoteUser != null && !#remoteUser.isEmpty()">
    <s:a action="login">Login</s:a>
</s:if>
<s:else>
    <s:a action="logout">Logout</s:a>
</s:else>

With JSTL

<c:if test="${empty pageContext.request.remoteUser}">
    <s:a action="login">Login</s:a>
</c:if>
<c:if test="${not empty pageContext.request.remoteUser}">
    <s:a action="logout">Logout</s:a>
</c:if>

or

<c:choose>
    <c:when test="${empty pageContext.request.remoteUser}">
        <s:a action="login">Login</s:a>
    </c:when>
    <c:otherwise>
        <s:a action="logout">Logout</s:a>
    </c:otherwise>
</c:choose>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜