Based on the user session I have to display different stuff on JSP.
Something is wrong, please advice
<c:if test="${session.getAttribute("userEmail")}">
<c:choose>
<c:when test="Ansari@precise-one.com"> //here if test is not null
<tr>
<td colspan="1" width="40%" nowrap="nowrap" style="text-align: left;">
<img src="<%=request.getContextPath()%>/images/icons/chevron_double.gif"/>
<a href="http://www.google.开发者_如何学Gocom" class="underlinedLinksLic">Google</a>
<img src="<%=request.getContextPath()%>/images/icons/chevron_double.gif"/>
<a href="http://www.gmail.com" class="underlinedLinksLic">Gmail</a></td>
</tr>
</c:when>
<c:when test="test@liferay.com"> //here if session is null
<tr>Hello</tr>
</c:when>
<tr height="3px;">
<td> </td>
</tr>
</c:choose>
<c:if test="${session.getAttribute("userEmail")}">
This ain't going to work. This should have been
<c:if test="${not empty userEmail}">
The <c:if>
block will then be entered whenever the scoped attribute userEmail
is not null
or empty string. To show some stuff when the userEmail
attribute is null
or empty string, then do so:
<c:if test="${empty userEmail}">
Further, those String comparisons
<c:when test="Ansari@precise-one.com">
<c:when test="test@liferay.com">
should have been
<c:when test="${userEmail == 'Ansari@precise-one.com'}">
<c:when test="${userEmail == 'test@liferay.com'}">
Knowing those facts, you should be able to reconstruct your <c:if>
and <c:choose>
blocks more appropriately. Here's an example:
<c:if test="${not empty userEmail}">
This block will be displayed when attribute `userEmail` is not empty.
<c:choose>
<c:when test="${userEmail == 'Ansari@precise-one.com'}">
This block will be displayed when `userEmail` matches Ansari@precise-one.com.
</c:when>
<c:when test="${userEmail == 'test@liferay.com'}">
This block will be displayed when `userEmail` matches test@liferay.com.
</c:when>
<c:otherwise>
There must always be a `c:otherwise` block.
This will be displayed when attribute `userEmail` doesn't match anything.
</c:otherwise>
</c:choose>
</c:if>
<c:if test="${empty userEmail}">
This block will be displayed when attribute `userEmail` is empty.
</c:if>
See also:
- Java EE 5 tutorial - JSP Standard Tag Library (JSTL)
- Java EE 5 tutorial - Examples of EL expressions
Try to use session object (depends on how you mark in session that user logged in):
session.getAttribute("logged-in") == true
精彩评论