jstl中forEach标签的一些常规用法

在jsp中潜入jstl标签,通常需要配置jstl的jar包,还需要在jsp的声明处利用jsp的tanlib指令引入JSTL的标签库
在jsp中使用EL技术和JSTL技术是为了减少jsp脚本代码的编写
下面直接上代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import = "java.util.*" %>
<%@page import ="com.auicyh.domain.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%
		//模拟list数组
		List<String> strList = new ArrayList<String>();
		strList.add("研发中心");
		strList.add("事业中心");
		strList.add("财务中心");
		strList.add("供应中心");
		request.setAttribute("strList", strList);

		//遍历List<User>的值
		List<User> userList = new ArrayList<User>();
		User user1 = new User();
		user1.setUid(1);
		user1.setUname("lisi");
		user1.setUpassword("123");
		userList.add(user1);
		User user2 = new User();
		user2.setUid(3);
		user2.setUname("wangwu");
		user2.setUpassword("123");
		userList.add(user2);
		application.setAttribute("userList", userList);

		//遍历Map<String,String>的值
		Map<String, String> strMap = new HashMap<String, String>();
		strMap.put("name", "lucy");
		strMap.put("age", "18");
		strMap.put("addr", "西三旗");
		strMap.put("email", "licy@itcast.cn");
		session.setAttribute("strMap", strMap);

		//遍历Map<String,User>的值
		Map<String, User> userMap = new HashMap<String, User>();
		userMap.put("user1", user1);
		userMap.put("user2", user2);
		request.setAttribute("userMap", userMap);
	%> 
	<h1>取出List集合中的数据</h1>
	<c:forEach items="${strList }" var="str">
		${str }
	</c:forEach>
	
	<h1>取出userList的数据</h1>
	<c:forEach items="${userList }" var="user">
		${user.uname }:${user.upassword }<br>
	</c:forEach>
	<h1>取出strMap的数据</h1>
	<c:forEach items="${strMap }" var="entry">
		${entry.key }====${entry.value }<br/>
	</c:forEach>
	
	<h1>取出userMap的数据</h1>
	<c:forEach items="${userMap }" var="entry">
		${entry.key }:${entry.value.uname }--${entry.value.upassword }<br/>
	</c:forEach>
</body>
</html>

阅读更多
换一批

没有更多推荐了,返回首页