Map在jsp页面中的遍历(Map<String, List<T>>)

JAVA代码

if(StringUtil.isNotNull(businessId)){
			List<AppProjPropLink> linkList = projTypeService.getCusPropByProjTypeId(businessId, metaCode);
			Map<String, List<AppProjPropLink>> map = new HashMap<String, List<AppProjPropLink>>();
			for (AppProjPropLink appProjPropLink : linkList) {
				if(!StringUtil.isNotNull(appProjPropLink.getMetaProp().getDictSelfGroup())){
					if(!StringUtil.isNotNull(map.get("未分组"))){
						List<AppProjPropLink> tempList = new ArrayList<AppProjPropLink>();
						tempList.add(appProjPropLink);
						map.put("未分组", tempList);
					}else{
						List<AppProjPropLink> tempList = map.get("未分组");
						tempList.add(appProjPropLink);
						map.put("未分组", tempList);
					}
				}else{
					if(!StringUtil.isNotNull(map.get(appProjPropLink.getMetaProp().getDictSelfGroup().getName()))){
						List<AppProjPropLink> tempList = new ArrayList<AppProjPropLink>();
						tempList.add(appProjPropLink);
						map.put(appProjPropLink.getMetaProp().getDictSelfGroup().getName(), tempList);
					}else{
						List<AppProjPropLink> tempList = map.get(appProjPropLink.getMetaProp().getDictSelfGroup().getName());
						tempList.add(appProjPropLink);
						map.put(appProjPropLink.getMetaProp().getDictSelfGroup().getName(), tempList);
					}
				}
			}
			model.addAttribute("map", map);
		}

js代码
<form id="propertyListForm" method="post">
    <input type="hidden" name="projTypeId" id="projTypeId" value="${businessId}"/>
    	<table align="center" width="100%">
    		<tr>
    			<td width="40%">属性名称</td>
    			<td width="30%">是否选择</td>
    			<td width="30%">是否必填</td>
    		</tr>
    	</table>
    		<c:forEach items="${map}" var="entry">
    			<fieldset>
				    <legend>${entry.key}:</legend>
				    <table align="center" width="100%">
				    	<c:forEach items="${entry.value }" var="link">
				    	<tr>
		    				<td width="40%"><label>${link.metaProp.name}</label></td>
		    				<td width="30%"><input type="checkbox" name="isChecked" value="${link.metaProp.id}" <c:if test="${link.id ne null}">checked="checked"</c:if> οnclick="setDisabled('${link.metaProp.id}')"/></td>
		    				<td width="30%"><input id="${link.metaProp.id}" type="checkbox" name="isRequired" <c:if test="${link.id eq null}">disabled="true"</c:if> <c:if test="${link.isRequired eq '1'}">checked="checked"</c:if>/></td>
					    </tr>
					    </c:forEach>
				    </table>
				</fieldset>
			</c:forEach>
    </form>


好的,以下是一个使用`List<Map>`将数据库信息展示在前端的示例Java代码: ```java // 定义一个获取数据库所有信息的方法 public List<Map<String, Object>> getAllData() { Connection conn = null; Statement stmt = null; ResultSet rs = null; List<Map<String, Object>> dataList = new ArrayList<>(); try { // 获取数据库连接 conn = DriverManager.getConnection(DB_URL, USER, PASS); // 执行查询语句 stmt = conn.createStatement(); String sql = "SELECT * FROM table_name"; rs = stmt.executeQuery(sql); // 处理结果集 ResultSetMetaData metaData = rs.getMetaData(); int columnCount = metaData.getColumnCount(); while (rs.next()) { Map<String, Object> dataMap = new HashMap<>(); for (int i = 1; i <= columnCount; i++) { dataMap.put(metaData.getColumnName(i), rs.getObject(i)); } dataList.add(dataMap); } } catch (SQLException e) { e.printStackTrace(); } finally { // 关闭资源 try { if (rs != null) { rs.close(); } if (stmt != null) { stmt.close(); } if (conn != null) { conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } return dataList; } ``` 在这个方法,我们通过执行查询语句获取数据库的所有数据,并将结果保存在一个`List<Map>`。每个`Map`对象表示一条数据,其`Map`的key表示列名,value表示对应的值。 接下来,我们将这个`List<Map>`传递给前端页面进行展示。以下是一个简单的JSP页面示例,在这个页面,我们使用JSTL标签库来遍历`List<Map>`,并将每个`Map`的数据展示出来: ```jsp <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html> <html> <head> <title>Database Data</title> </head> <body> <table> <thead> <tr> <th>Column 1</th> <th>Column 2</th> <th>Column 3</th> </tr> </thead> <tbody> <c:forEach var="data" items="${dataList}"> <tr> <td>${data.column1}</td> <td>${data.column2}</td> <td>${data.column3}</td> </tr> </c:forEach> </tbody> </table> </body> </html> ``` 在这个JSP页面,我们使用`<c:forEach>`标签来遍历`List<Map>`的每个`Map`对象,并使用`${data.key}`的方式来获取每个key对应的值。这样就可以将数据库的数据展示在前端页面上了。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值