JSTL 迭代列表创建字段集
大家好,我目前正在遍历一个列表,并根据字段集是否符合特定条件对字段集进行硬编码。如何编写一个循环,根据盖子中的所有组名创建一个字段集,并使用与该组名动态配合的所有相应的显示名称填充该字段集。我目前对此进行了硬编码。
<%@ include file="../include/pre-header.html" %>
<tr>
<th>
<span onclick="toggleDiv('displayFields', 'displayImg')" style="cursor: hand;">Data Fields <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);"/>
<input type="button" name="clear_choice" value="Uncheck All" onclick="checkUncheck(false);"/>
</td>
</tr>
</td>
</tr>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议将您的数据结构从
List
更改为Map>
,其中组名称是映射键。下面的示例展示了如何转换它:
(当然,您也可以在创建原始
detFields
列表时更改数据结构)您可能还想维护所有标头名称的映射
(最好在 web 应用程序启动时创建一次并将其存储在应用程序范围内,您可以为此使用
ServletContextListener
)您可以使用单个嵌套
。Map
上的每次迭代都会给出Map.Entry
后面又具有getKey()
和getValue()
方法。键是String
组名称,值是List
。I'd suggest to change your datastructure from
List<DetBean>
toMap<String, List<DetBean>>
where the group name is the map key.The below example shows how to convert it:
(you can of course also change the datastructure at the point where you're creating the original
detFields
list)You probably also want to maintain a mapping of all header names
(it would probably be better to create it once on webapp's startup and store in application scope, you can use a
ServletContextListener
for this)This way you can use a single nested
<c:forEach>
. Every iteration over aMap
givesMap.Entry
back which in turn hasgetKey()
andgetValue()
methods. The key is then theString
group name and the value is then theList<DetBean>
.