【问题标题】:java, JSTL and getting values by keyjava,JSTL和按键获取值
【发布时间】:2015-04-24 08:36:19
【问题描述】:

我想知道为什么我不能使用 jstl 中的键从外部哈希表中获取值,键是整数值,值是内部哈希表,我正在处理一些遗留代码,因此原因哈希表,以这种形式从存储过程调用返回。

奇怪的是,在我的 forEach 循环中处理内部哈希表时,我能够得到这些值...${data['NAME']} 实际上确实有效。

我可以使用 JSTL forEach 循环遍历整个外部哈希表,并且可以,但是如果我尝试获取像 ${missing_ciphers[1]} 甚至 ${missing_ciphers['1']} 这样的值,则不会返回任何内容。

打印时的哈希表如下所示:

{4={SOURCE=D, NAME=D}, 3={SOURCE=C, NAME=C}, 2={SOURCE=B, NAME=B},   1={SOURCE=A, NAME=A}}

我可以使用以下代码循环遍历它,但列表不是我想要的顺序,所以我想使用循环计数器通过它们的键取出对象,但这似乎没有返回任何东西(一旦我按照我的意愿工作,内联样式将被移动到一个 css 文件中......):

<c:forEach items="${missing_ciphers}" var="ciphers" varStatus="cipher_loop">

                        <c:set var="data" value="${ciphers.value}">
                        </c:set>

                        <tr style="border-left: none; border-right: none;" class="${cipher_loop.index % 2 == 0 ? 'even' : 'odd'}">
                            <td><span style="font-weight: bold;">${data['SOURCE']}</span></td>
                            <td><span style="font-weight: bold;">${data['NAME']}</span></td>
                        </tr>
                    </c:forEach> 

谁能帮助我了解这里发生了什么以及为什么我无法使用 ${missing_ciphers[1]} 类型语法获取值?

我应该补充一点,以下代码确实将内部哈希表打印到 tomcat 控制台:

&lt;% System.out.println("val: " + ((Hashtable)request.getAttribute("missing_ciphers")).get(1)); %&gt;

我什至尝试了以下方法,看看它是否归结为仍然无法从外部哈希表中获取的键的类型:

<c:forEach items="${missing_ciphers}" var="ciphers" varStatus="cipher_loop">
                        <c:set var="counter" value="${cipher_loop.index + 1}" />
                        <fmt:parseNumber var="fmt_counter" integerOnly="true" type="number" value="${counter}" />
                        <c:out value="${fmt_counter}" />

                        <c:set var="data" value="${missing_ciphers[fmt_counter]}"></c:set>

                        <tr style="border-left: none; border-right: none;" class="${cipher_loop.index % 2 == 0 ? 'even' : 'odd'}">
                            <td><span style="font-weight: bold;">${data['SOURCE']}</span></td>
                            <td><span style="font-weight: bold;">${data['NAME']}</span></td>
                        </tr>
                    </c:forEach>

谢谢

【问题讨论】:

    标签: java foreach jstl hashtable


    【解决方案1】:

    您应该使用分配给 varStatus 的变量名称 - 'cipher_loop' 而不是 varStatus 本身。

    您还应该使用 index 属性或 count 属性来获取当前索引。 (索引从 0 开始,默认从 1 开始计数)

    ${missing_ciphers[varStatus]}
    
    should be 
    
    ${missing_ciphers[cipher_loop.count]}
    

    编辑

    你的外键是什么类型的?整数?

    varStatus.index(或计数器)的类型很长,您必须将其显式设置为整数才能将其用作外部表的键

    以下代码对我有用

    <jsp:directive.page import="java.util.*"/>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <%
    
    Hashtable h = new Hashtable();
    Hashtable h1 = new Hashtable();h1.put("SOURCE","D");h1.put("NAME","D");
    Hashtable h2 = new Hashtable();h2.put("SOURCE","C");h2.put("NAME","C");
    Hashtable h3 = new Hashtable();h3.put("SOURCE","B");h3.put("NAME","B");
    Hashtable h4 = new Hashtable();h4.put("SOURCE","A");h4.put("NAME","A");
    h.put(4,h1);h.put(3,h2);h.put(2,h3);h.put(1,h1);
    
    
    request.setAttribute("missing_ciphers",h);
    
    %>
    
    
    <c:forEach items="${missing_ciphers}" var="ciphers" varStatus="cipher_loop">
            <c:set var="counter" value="${cipher_loop.index + 1}" />
            <c:out value = "${counter}"/>
            <c:set var = 'counter' value = '<%=new Integer(pageContext.findAttribute("counter").toString())%>'/>
    
            <c:set var="data" value="${missing_ciphers[counter]}"></c:set>
    
    
                            <tr>
                                <td><span style="font-weight: bold;">${data['SOURCE']}</span></td>
                                <td><span style="font-weight: bold;">${data['NAME']}</span></td>
                            </tr>
    </c:forEach>
    

    OTOH,如果您的表中的键是 long 类型,那么您可以直接使用 varstatus.index 属性

    <jsp:directive.page import="java.util.*"/>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <%
    
    Hashtable h = new Hashtable();
    Hashtable h1 = new Hashtable();h1.put("SOURCE","D");h1.put("NAME","D");
    Hashtable h2 = new Hashtable();h2.put("SOURCE","C");h2.put("NAME","C");
    Hashtable h3 = new Hashtable();h3.put("SOURCE","B");h3.put("NAME","B");
    Hashtable h4 = new Hashtable();h4.put("SOURCE","A");h4.put("NAME","A");
    h.put(new Long(4),h1);h.put(new Long(3),h2);h.put(new Long(2),h3);h.put(new Long(1),h1);
    
    
    request.setAttribute("missing_ciphers",h);
    
    %>
    test
    
    
    <<c:forEach items="${missing_ciphers}" var="ciphers" varStatus="cipher_loop">
            <c:set var="counter" value="${cipher_loop.index + 1}" />
    
    
            <c:set var="data" value="${missing_ciphers[counter]}"></c:set>
            ${data}
                            <tr>
                                <td><span style="font-weight: bold;">${data['SOURCE']}</span></td>
                                <td><span style="font-weight: bold;">${data['NAME']}</span></td>
                            </tr>
    </c:forEach>
    

    【讨论】:

    • 嗨对不起,这不应该是我的例子的一部分......我还是用你的代码替换了它,它什么也没做,然后我尝试了 ${missing_ciphers[cipher_loop.index + 1]} 因为索引是我用于备用行着色的方法,它也不会从哈希表返回任何内容。
    猜你喜欢
    • 2013-09-16
    • 1970-01-01
    • 1970-01-01
    • 2020-03-28
    • 2012-09-23
    • 2011-01-27
    • 1970-01-01
    相关资源
    最近更新 更多