如何将一个ArrayList分解并显示到多个表列中

时间:2013-03-21 15:25:00

标签: java jquery jsp servlets displaytag

我有一个servlet,它加载一个属性文件,并在ArrayList对象中包含100个测试用例名称的列表。将servlet加载到JSP后,该JSP在表中显示列表。列表很长,所以我想要一些优雅的方式将它显示在一个表中,以便它分成例如JSP上的三列或四列。

我现在所做的是将列表分解为servlet中的三个子列表:

//load properties
Properties props = new Properties();
        ArrayList<String> tests = new ArrayList<String>();
        props.load(getServletContext().getResourceAsStream("/WEB-INF/sailcertifier.properties"));
        Pattern pattern = Pattern.compile("[A-Z]{3}-[0-9]{2}");     
        for (Enumeration<Object> e = props.keys(); e.hasMoreElements();) {
            String key = (String) e.nextElement();
            Matcher m = pattern.matcher(key);
            if (m.find())
                tests.add(key);
        }
        Collections.sort(tests, new TestOrderComparator());
        confBean.setPossibleTests(tests.toArray(new String[tests.size()]));
        int third = tests.size() / 3;
        List<String> testSubset1 = tests.subList(0, third);
        List<String> testSubset2 = tests.subList(third, third * 2);
        List<String> testSubset3 = tests.subList(third * 2, tests.size());
        //store the bean as a request attribute
        request.setAttribute("testSet1", testSubset1.toArray(new String[testSubset1.size()]));
        request.setAttribute("testSet2", testSubset2.toArray(new String[testSubset2.size()]));
        request.setAttribute("testSet3", testSubset3.toArray(new String[testSubset3.size()]));
        request.setAttribute("testsConf", confBean);
        request.setAttribute("certProps", props);
        //forward to tests selection page
        String url = "/sailcertifier/jsp/testsSelection.jsp";
        RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
        response.setContentType("application/javascript");
        try {
            dispatcher.forward(request, response);
        } catch (ServletException e) {
            e.printStackTrace();
        }

在JSP中,我像这样遍历子列表(为每个案例添加一些html元素):

    <table>
        <tr>
            <td style="width: 33%">
                <table>
                    <tr>
                        <td>
                            <c:forEach var="testName" items="${testSet1}">
                                <tr>
                                    <td><label for="${testName}" id=${testName}Label>${testName}</label></td>
                                    <td><input id=${testName} type="checkbox" value=${testName} name="selTest"></input></td>
                                    <td><input id=${testName}Run type="button" value="Run Test" name="runButtons" /></td>
                                    <td><input id=${testName}ManPass type="button" value="Manual Pass" name="manPassButtons"/></td>
                                    <td><div id=${testName}Status style="width:100px"></td>
                                    <td></td>
                                    <td></td>
                                </tr>
                            </c:forEach>
                            <tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>
                        </td>
                    </tr>
                </table>
            </td>
            <td style="width: 33%">
                <table>
                    <tr>
                        <td>
                            <c:forEach var="testName" items="${testSet2}">
                                <tr>
                                    <td><label for="${testName}" id=${testName}Label>${testName}</label></td>
                                    <td><input id=${testName} type="checkbox" value=${testName} name="selTest"></input></td>
                                    <td><input id=${testName}Run type="button" value="Run Test" name="runButtons" /></td>
                                    <td><input id=${testName}ManPass type="button" value="Manual Pass" name="manPassButtons"/></td>
                                    <td><div id=${testName}Status style="width:100px"></td>
                                    <td></td>
                                    <td></td>
                                </tr>
                            </c:forEach>
                            <tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>
                        </td>
                    </tr>
                </table>
            </td>
            <td style="width: 33%">
                <table>
                    <tr>
                        <td>
                            <c:forEach var="testName" items="${testSet3}">
                                <tr>
                                    <td><label for="${testName}" id=${testName}Label>${testName}</label></td>
                                    <td><input id=${testName} type="checkbox" value=${testName} name="selTest"></input></td>
                                    <td><input id=${testName}Run type="button" value="Run Test" name="runButtons" /></td>
                                    <td><input id=${testName}ManPass type="button" value="Manual Pass" name="manPassButtons"/></td>
                                    <td><div id=${testName}Status style="width:100px"></td>
                                    <td></td>
                                </tr>
                            </c:forEach>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>

我承认使用三个JSTL for循环是一种丑陋(还没想到使用完整的原始列表)。是否有一种更清晰的方法来处理这个问题,通过使用一些jquery插件(如jquery grid plugin)或其他库(displaytag?)来为我平均分割成行?

2 个答案:

答案 0 :(得分:6)

打印<tr>,然后循环遍历数组列表,然后打印<td>中的每个项目并打印</tr><tr>每个 n 项目,然后结束数组列表上的循环,最后打印</tr>

E.g。每隔3个项目换一个新行:

<table>
    <tr>
        <c:forEach items="${items}" var="item" varStatus="loop">
            <c:if test="${not loop.first and loop.index % 3 == 0}">
                </tr><tr>
            </c:if>
            <td>${item}</td>
        </c:forEach>
    </tr>
</table>

${loop.index}返回当前迭代轮次的索引。 % 3只会返回0,如果它可以被3分割而没有余数(因此,当索引为0,3,6,9,12等时)。

答案 1 :(得分:0)

我不确定我是否理解了您的请求,但假设您拥有100个元素数组并且您希望显示3列,那么您可以利用&#34; float&#34;财产实现: 只需创建一个你想要的宽度的div,然后遍历数组,使内部div为width / 3:

     <div style="width:600px;margin:auto;">
          <c:forEach var="testName" items="${testSet}">
              <div style="float:left;width:200px;">whatever you want to do with ${testName}</div>
          </c:forEach>
     </div>