TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
刚接触JSTL和EL表达式,不知道如何从List<Map<String,Object>>取值,用来循环显示。经过半个小时的尝试,发现其实很简单,与大家分享下。
<%@ page language="java" pageEncoding="UTF-8" import="java.util.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
[code][/code]
<%@ page language="java" pageEncoding="UTF-8" import="java.util.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
<html:base />
<title>MyJsp.jsp</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<!-- 测试用数据,一般通过request.attribute跳转过来-->
<%
List<Map<String,Object>> students = new ArrayList<Map<String,Object>>();
Map<String,Object> s1 = new HashMap<String,Object>();
s1.put("name","jim");
s1.put("age","15");
students.add(s1);
Map<String,Object> s2 = new HashMap<String,Object>();
s2.put("name","lucy");
s2.put("age","12");
students.add(s2);
request.setAttribute("students",students);
%>
<body>
<table>
<tr>
<td>姓名
</td>
<td>年龄
</td>
</tr>
<!-- loop begin -->
<c:forEach var="student" items="$ {students}" >
<tr>
<td> <c:out value="$ {student.name}" default="wang"/>
</td>
<td>
<c:out value="$ {student.age}" default="wang"/>
</td>
</tr>
</c:forEach>
<!-- loop end -->
</table>
</body>
</html:html>
运行结果如下:
姓名 年龄
jim 15
lucy 12
有了EL表达式确实很方便。微笑
运行结果如下:
姓名 年龄
jim 15
lucy 12
有了EL表达式确实很方便。
本文出处:http://www.byywee.com/page/M0/S544/544304.html |
|