jstl标签2

时间:2023-03-08 18:02:33
jstl标签2
我们前面讲的是ArrayList集合迭代,Hashmap/Hashset的情况下
举例:
<h3>对map和set的迭代</h3>
放入字符串
<%
//模拟
Map map=new HashMap();
map.put("a", "宝强");
map.put("b", "马蓉");
request.setAttribute("persons", map);
%>
<c:forEach items="${persons}" var="per">
key=${per.key} 值=${per.value}
</c:forEach> </br>
<h3>放入一个对象</h3>
<%
//模拟
Map map1=new HashMap();
map1.put("u1", u1);
map1.put("u2", u2);
request.setAttribute("users",map1);
%>
<c:forEach items="${users}" var="user">
key=${user.key} 值=${user.value.name} password=${user.value.password}
</c:forEach> </br>
<h3>set集合</h3>
<%
//模拟
Set set=new HashSet();
set.add(u1);
set.add(u2);
request.setAttribute("users1",set);
%>
<c:forEach items="${users1}" var="user1">
值=${user1.name} password=${user1.password}
</c:forEach> <h2>如何使用后jstl if区判断集合是否为空</h2>
<c:if test="${empty users1}">
没有人
</c:if> <h2>redirect</h2>
<c:redirect url="http://www.sohu.com"></c:redirect> <h2>import</h2>
<c:import url="a.jsp">
<!-- 它的功用主要是:可以将参数传递给被包含的文件,它有两个属性name和value. -->
<c:param name="name" value="xkj"></c:param>
<!-- 相当于a.jsp?name=xkj -->
</c:import>
a.jsp中:
${param.name}可取出属性值。