自定义 JSP 页面

发布于 2024-10-14 09:30:33 字数 1442 浏览 3 评论 0原文

根据用户会话,我必须在 JSP 上显示不同的内容。

有问题请指教

  <c:if test="${session.getAttribute("userEmail")}">

        <c:choose>
        <c:when test="[email protected]"> //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.com" class="underlinedLinksLic">Google</a>

            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <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="[email protected]">   //here if session is null
        <tr>Hello</tr>  
        </c:when>

        <tr height="3px;">
            <td>&nbsp;</td>
        </tr>

    </c:choose> 

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="[email protected]"> //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.com" 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="[email protected]">   //here if session is null
        <tr>Hello</tr>  
        </c:when>

        <tr height="3px;">
            <td> </td>
        </tr>

    </c:choose> 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

江湖正好 2024-10-21 09:30:33
<c:if test="${session.getAttribute("userEmail")}">

这是行不通的。这应该是

<c:if test="${not empty userEmail}">

只要作用域属性 userEmail 不是 null 或空字符串,就会输入 块。要在 userEmail 属性为 null 或空字符串时显示一些内容,请执行以下操作:

<c:if test="${empty userEmail}">

此外,这些字符串比较

<c:when test="[email protected]">
<c:when test="[email protected]">  

应该已经

<c:when test="${userEmail == '[email protected]'}">
<c:when test="${userEmail == '[email protected]'}">  

了解了这些事实,您应该能够重构您的 块更合适。下面是一个示例:

<c:if test="${not empty userEmail}">
    This block will be displayed when attribute `userEmail` is not empty.

    <c:choose>
        <c:when test="${userEmail == '[email protected]'}">
            This block will be displayed when `userEmail` matches [email protected].
        </c:when>
        <c:when test="${userEmail == '[email protected]'}">  
            This block will be displayed when `userEmail` matches [email protected].
        </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>

另请参阅:

<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="[email protected]">
<c:when test="[email protected]">  

should have been

<c:when test="${userEmail == '[email protected]'}">
<c:when test="${userEmail == '[email protected]'}">  

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 == '[email protected]'}">
            This block will be displayed when `userEmail` matches [email protected].
        </c:when>
        <c:when test="${userEmail == '[email protected]'}">  
            This block will be displayed when `userEmail` matches [email protected].
        </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:

遮云壑 2024-10-21 09:30:33

尝试使用会话对象(取决于您如何在用户登录的会话中标记):

session.getAttribute("logged-in") == true

Try to use session object (depends on how you mark in session that user logged in):

session.getAttribute("logged-in") == true
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文