我有一个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进行循环有点难看(尚未弄清楚是否使用完整的原始列表)。是否有更清洁的方法来解决此问题,方法是使用某些jquery插件(例如jquery grid plugin)或其他库(displaytag吗?)为我平均处理分成几行?
打印a <tr>,然后循环遍历数组列表,然后打印<td>a中的</tr><tr>每一项并打印每 n个 项目,然后结束循环遍历数组列表,最后再打印一次</tr>。
<tr>
<td>
</tr><tr>
</tr>
例如,每第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,等等)。
${loop.index}
% 3
0