【问题标题】:multiple <c:when in <c:choose + cookies多个 <c:when in <c:choose + cookies
【发布时间】:2014-01-02 12:45:24
【问题描述】:

您好,我正在尝试创建一个有序列表,该列表可以显示选项以根据 cookie 的值移动到各个页面。 cookie 已正确设置。并且它的价值正在获得..但有一些我无法准确指出的错误..

     <c:forEach items= "${cookie}" var="currentCookie">
    <c:if test="${currentCookie.key eq 'user_type' }">
    <c:set var="user_type" value = "${currentCookie.value.value }"/>
    </c:if>
     </c:forEach>



     <c:choose>
<c:when test="${not empty user_type}">
    <li><a class="" href="#">SELECT</a>
    <ul>
    <c:when test="${user_type='T'}"><li><a href="/myproject/f/auction_history/">My Account</a></li></c:when>
    <c:when test="${user_type='P'}"><li><a href="/myproject/f/auction_history/">My Account</a></li></c:when>
    <c:when test="${user_type='D'}"><li><a href="/myproject/f/menubarCreateAuction/">Create Auction</a></li></c:when>
    <c:when test="${user_type='F'}"><li><a href="/myproject/f/menubarDfoManagement/">DFO Management</a></li></c:when>
    <c:when test="${user_type='S'}"><li><a href="/myproject/f/menubaruserManagement/">User Management</a></li></c:when>
    </ul>
    </li>
</c:when>
<c:otherwise>
    <li><a class="" href="#">SELECT</a>
    <ul>
        <li><a href="/myproject/f/menubarCreateAuction/">Create Auction</a></li>
        <li><a href="/myproject/f/menubarDfoManagement/">DFO Management</a></li>
        <li><a href="/myproject/f/menubaruserManagement/">User Management</a></li>
    </ul>
    </li>

</c:otherwise>

foreach 选择各种cookies,获取带有user_type 的那个并获取值..

在 c 中选择 cookie 的值与生成有序列表的预定值进行比较 如果未选择任何值,则列表中应仅显示 3 个项目

请帮忙..

【问题讨论】:

  • 您的“测试”语法可能需要查看。请改用${user_type eq 'T'}。注意eq 的使用。你也可以使用==
  • 其实这里还有很多错误。您对&lt;li&gt;&lt;ul&gt; 的使用完全搞砸了,您对&lt;c:when&gt; 语句的嵌入是错误的,嵌套而没有包装&lt;c:choose&gt;,然后您对排序列表的要求有点奇怪,因为您要么有一个值或 3,但仅此而已。

标签: java list jsp cookies jstl


【解决方案1】:

以下代码似乎没问题,它将检索“user_type”cookie 的最后一次出现的值。

<c:forEach items= "${cookie}" var="currentCookie">
    <c:if test="${currentCookie.key eq 'user_type' }">
        <c:set var="user_type" value = "${currentCookie.value.value }"/>
    </c:if>
</c:forEach>

应该注意,它永远只有一个值。如果您想要更多,您需要将您的“选项”放在foreach 中。

<c:choose>
<c:when test="${!empty user_type}">
    <ul>
        <li><a class="" href="#">SELECT</a></li>
        <c:choose>
                <c:when test="${user_type eq 'T'}">
                   <li><a href="/myproject/f/auction_history/">My Account</a></li>
                </c:when>
                <c:when test="${user_type eq 'P'}">
                   <li><a href="/myproject/f/auction_history/">My Account</a></li>
                </c:when>
                <c:when test="${user_type eq 'D'}">
                   <li><a href="/myproject/f/menubarCreateAuction/">Create Auction</a></li>
                </c:when>
                <c:when test="${user_type eq 'F'}">
                   <li><a href="/myproject/f/menubarDfoManagement/">DFO Management</a></li>
                </c:when>
                <c:when test="${user_type eq 'S'}">
                   <li><a href="/myproject/f/menubaruserManagement/">User Management</a></li>
                </c:when>
                <c:otherwise>
                     <!-- And what if the type is NOT one of your options? -->
                </c:otherwise>
             <c:choose>
       </ul>
</c:when>
<c:otherwise>
  <ul>
   <li><a class="" href="#">SELECT</a>
   <li><a href="/myproject/f/menubarCreateAuction/">Create Auction</a></li>
   <li><a href="/myproject/f/menubarDfoManagement/">DFO Management</a></li>
   <li><a href="/myproject/f/menubaruserManagement/">User Management</a></li>
  </ul>
</c:otherwise>

在上面的代码中,做了如下改动:

  • 添加了结束 &lt;/c:choose&gt; 更正了 &lt;ul&gt;&lt;li&gt; 标签的使用,因为这是不正确的
  • 更正了test 语句以使用eq
  • 为条件选项添加了嵌套的&lt;c:choose&gt;
  • 添加了一条评论,以考虑 user_type 的状态不是空的,但也不是提供的选项之一。

您需要重新访问应用程序中的流程,因为我认为您的代码不符合您的要求。

【讨论】:

    【解决方案2】:

    我解决了.... :D:D:D 问题出在 c:choose.. 这完全是不必要的...... 更正后的代码是

                <c:forEach items= "${cookie}" var="currentCookie">
                            <c:if test="${currentCookie.key eq 'user_type' }">
                                <c:set var="user_type" value = "${currentCookie.value.value }"/>
    
                            </c:if>
                        </c:forEach> 
    
          <c:if test="${user_type eq 'T' || user_type eq 'P'}"> 
                                 <li><a href="/myproject/f/auction_history/">My Account</a></li>
                        </c:if>                                         
                        <c:if test="${user_type eq 'D'}">
                                 <li><a href="/myproject/f/menubarCreateAuction/">Create Auction</a></li>
                        </c:if>
                        <c:if test="${user_type eq 'F'}">
                                 <li><a href="/myproject/f/menubarDfoManagement/">DFO Management</a></li>
                        </c:if>
                        <c:if test="${user_type eq 'S'}">
                                 <li><a href="/myproject/f/menubaruserManagement/">User Management</a></li>
                        </c:if>
    

    radimpe.. 感谢您的意见... :)

    【讨论】:

      猜你喜欢
      • 2011-11-23
      • 2011-12-15
      • 2011-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-26
      • 1970-01-01
      • 2022-12-27
      相关资源
      最近更新 更多