JSTL迭代列表创建字段集

时间:2012-02-13 16:13:46

标签: jsp jstl

大家好我正在迭代列表和im硬编码字段集,基于它们是否符合某个标准。如何根据lidst中的所有组名创建一个创建字段集的循环,并使用与该groupName动态相关的所有相应displayNames填充该fieldset。我目前正在硬编码。

<%@ include file="../include/pre-header.html" %>

    <tr>
        <th>
            <span onclick="toggleDiv('displayFields', 'displayImg')" style="cursor: hand;">Data Fields&nbsp;<img name="displayImg" src="../images/minus.gif" /></span>
        </th>

    </tr>

    <tr>
        <td>

            <div id="displayFields" style="display:block;">
            <fieldset class="det">
                <legend>Header Data</legend>
                    <c:forEach var="detBean" items="${detFields}">
                        <c:if test="${detBean.groupName == 'HEADER_DATA'}">
                    <input type="checkbox" name="fieldNames" value="${detBean.fieldName}">${detBean.displayName}</input>
                            <br/>
                        </c:if>
                   </c:forEach>
            </fieldset>

            <fieldset class="det">
                <legend>Materiel Data</legend>
                    <c:forEach var="detBean" items="${detFields}">
                        <c:if test="${detBean.groupName == 'MATERIEL_DATA'}">
                    <input type="checkbox" name="fieldNames" value="${detBean.fieldName}">${detBean.displayName}</input>
                            <br/>
                        </c:if>
                    </c:forEach>
            </fieldset>

            <fieldset class="det">
                <legend>Planned Unit Data</legend>
                    <c:forEach var="detBean" items="${detFields}">
                        <c:if test="${detBean.groupName == 'PLANNED_DATA'}">
                    <input type="checkbox" name="fieldNames" value="${detBean.fieldName}">${detBean.displayName}</input>
                            <br/>
                        </c:if>
                    </c:forEach>
            </fieldset>

            <fieldset class="det">
                <legend>Actual Unit Data</legend>
                    <c:forEach var="detBean" items="${detFields}">
                        <c:if test="${detBean.groupName == 'ACTUAL_DATA'}">
                <input type="checkbox" name="fieldNames" value="${detBean.fieldName}">${detBean.displayName}</input>
                            <br/>
                        </c:if>
                </c:forEach>
            </fieldset>

            <fieldset class="det">
                <legend>Planned Cost Data</legend>
                <c:forEach var="detBean" items="${detFields}">
                    <c:if test="${detBean.groupName == 'COST_DATA'}">
                        <input type="checkbox" name="fieldNames" value="${detBean.fieldName}">${detBean.displayName}</input>
                        <br/>
                    </c:if>
                </c:forEach>
            </fieldset>

            <fieldset class="det">
                <legend>Carry Over Data</legend>
                    <c:forEach var="detBean" items="${detFields}">
                        <c:if test="${detBean.groupName == 'CARRYOVER_DATA'}">
                    <input type="checkbox" name="fieldNames" value="${detBean.fieldName}">${detBean.displayName}</input>
                        <br/>
                        </c:if>
                    </c:forEach>
            </fieldset>

            <fieldset class="det">
                <legend>Schedule Exceptions</legend>
                    <c:forEach var="detBean" items="${detFields}">
                        <c:if test="${detBean.groupName == 'SCHEDULE_EXCEPTIONS'}">
                    <input type="checkbox" name="fieldNames" value="${detBean.fieldName}">${detBean.displayName}</input>
                        <br/>
                        </c:if>
                    </c:forEach>
            </fieldset>

            <fieldset class="det">
                <legend>DIFMS data</legend>
                    <c:forEach var="detBean" items="${detFields}">
                        <c:if test="${detBean.groupName == 'DIFMS_DATA'}">
                    <input type="checkbox" name="fieldNames" value="${detBean.fieldName}">${detBean.displayName}</input>
                        <br/>
                        </c:if>
                    </c:forEach>
            </fieldset>
            </div>
            <tr>
            <td style="text-align: center;">
            <input type="button" name="clear_choice"  value="Check All" onclick="checkUncheck(true);"/>
            &nbsp;&nbsp;&nbsp;&nbsp;
            <input type="button" name="clear_choice"  value="Uncheck All" onclick="checkUncheck(false);"/>

            </td>
            </tr>

        </td>
        </tr>

1 个答案:

答案 0 :(得分:2)

我建议您将数据结构从List<DetBean>更改为Map<String, List<DetBean>>,其中组名称是地图密钥。

以下示例显示了如何转换它:

Map<String, List<DetBean>> detFieldMap = new LinkedHashMap<String, List<DetBean>>();

for (DetBean detBean : detFields) {
    String groupName = detBean.getGroupName();
    List<DetBean> detBeans = detFieldMap.get(groupName);

    if (detBeans == null) {
        detBeans = new ArrayList<DetBean>();
        detFieldMap.put(groupName, detBeans);
    }

    detBeans.add(detBean);
}

request.setAttribute("detFieldMap", detFieldMap);

(您当然也可以在创建原始detFields列表时更改数据结构)

您可能还希望维护所有标题名称的映射

Map<String, String> detFieldHeaders = new HashMap<String, String>();
detFieldHeaderMap.put("HEADER_DATA", "Header Data");
detFieldHeaderMap.put("MATERIEL_DATA", "Materiel Data");
// ...

request.setAttribute("detFieldHeaders", detFieldHeaders);

(在webapp的启动时创建一次并在应用程序范围内存储可能会更好,你可以使用ServletContextListener

这样您就可以使用单个嵌套<c:forEach>Map上的每次迭代都会返回Map.Entry,而后者又有getKey()getValue()方法。然后,关键是String组名称,然后值为List<DetBean>

<c:forEach items="${detFieldMap}" var="detFieldEntry">
    <fieldset class="det">
        <legend>${detFieldHeaders[detFieldEntry.key]}</legend>
        <c:forEach items="${detFieldEntry.value}" var="detBean">
            <input type="checkbox" name="fieldNames" value="${detBean.fieldName}">${detBean.displayName}</input>
            <br/>
        </c:forEach>
    </fieldset>
</c:forEach>