时间:2014-01-02 12:45:25

标签: java list jsp cookies jstl

您好我正在尝试创建一个有序列表,可以根据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选择各种cookie并获取具有user_type的cookie并获得值..

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

请帮助..

2 个答案:

答案 0 :(得分:0)

以下代码似乎没问题,并将检索“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>

在上面的代码中,进行了以下更改:

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

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

答案 1 :(得分:0)

我解决了......:D:D:D 问题出在c:选择..这是完全没有成功的...... 修正后的代码是

            <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 ..感谢您的投入......:)