首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

<c:foreach>

2012-06-26 
c:foreachJSTL之循环控制标签c:foreach博客分类: JSTL C++C#CJavaServlet.实例: JstlCoreServlet.java

<c:foreach>

JSTL之循环控制标签<c:foreach>
博客分类: JSTL

C++C#CJavaServlet.

实例:
JstlCoreServlet.java代码如下:
package com.bjpowernode.jstl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* 测试jstl核心库
* @author Administrator
*
*/
public class JstlCoreServlet extends HttpServlet {

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//普通字符串
request.setAttribute("hello", "hello world");

//html字符串
request.setAttribute("welcome", "<font color='red'>北京欢迎你</font>");

//条件控制标签
request.setAttribute("v1", 1);
request.setAttribute("v2", 2);
List userList = new ArrayList();
userList.add("user1");
userList.add("user2");
request.setAttribute("userlist", userList);

//结构
Group group = new Group();
group.setName("动力节点");
List users = new ArrayList();
for (int i=0; i<10; i++) {
User user = new User();
user.setUsername("User_" + i);
user.setAge(20 + i);
user.setGroup(group);
users.add(user);
}
request.setAttribute("users", users);

//map
Map map = new HashMap();
map.put("key1", "value1");
map.put("key2", "value2");
request.setAttribute("map", map);

//forTokens
request.setAttribute("strTokens", "a#b#c#d");

request.getRequestDispatcher("/jstl_core.jsp").forward(request, response);
}

}


jstl_core.jsp代码如下:
<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<%@ page import="java.util.*" %>   
<%@ page import="com.bjpowernode.jstl.*" %>   
<%@ 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=GB18030">
<title>Insert title here</title>
</head>
<body>
<h1>测试JSTL核心库</h1>
<hr>
<li>循环控制标签c:forEach</li><br>
<h1>采用jsp脚本取得用户列表</h1>
<table border="1">
<tr>
<td>姓名</td>
<td>年龄</td>
<td>所属组</td>
</tr>
<%
List users = (List)request.getAttribute("users");
if (users == null || users.size() == 0) {
%>
<tr>
<td colspan="3">没有符合条件的数据</td>
</tr>
<%
}else {
for (Iterator iter=users.iterator(); iter.hasNext();) {
User user = (User)iter.next();
%>
<tr>
<td><%=user.getUsername() %></td>
<td><%=user.getAge() %></td>
<td><%=user.getGroup().getName() %></td>
</tr>
<%
}
}
%>
</table>
<P>
<h1>采用jstl标签取得用户列表</h1>
<table border="1">
<tr>
<td>姓名</td>
<td>年龄</td>
<td>所属组</td>
</tr>
<c:choose>
<c:when test="${empty users}">
<tr>
<td colspan="3">没有符合条件的数据</td>
</tr>
</c:when>
<c:otherwise>
<c:forEach items="${users}" var="user">
<tr>
<td>${user.username }</td>
<td>${user.age }</td>
<td>${user.group.name }</td>
</tr>
</c:forEach>
</c:otherwise>
</c:choose>
</table>
<p>
<li>循环控制标签c:forEach,varstatus</li><br>
<table border="1">
<tr>
<td>姓名</td>
<td>年龄</td>
<td>所属组</td>
</tr>
<c:choose>
<c:when test="${empty users}">
<tr>
<td colspan="3">没有符合条件的数据</td>
</tr>
</c:when>
<c:otherwise>
<c:forEach items="${users}" var="user" varStatus="vs">
<c:choose>
<c:when test="${vs.count % 2 == 0}">
<tr bgcolor="red">
</c:when>
<c:otherwise>
<tr>
</c:otherwise>
</c:choose>
<td>${user.username }</td>
<td>${user.age }</td>
<td>${user.group.name }</td>
</tr>
</c:forEach>
</c:otherwise>
</c:choose>
</table>
<li>循环控制标签c:forEach,begin,end,step</li><br>
<table border="1">
<tr>
<td>姓名</td>
<td>年龄</td>
<td>所属组</td>
</tr>
<c:choose>
<c:when test="${empty users}">
<tr>
<td colspan="3">没有符合条件的数据</td>
</tr>
</c:when>
<c:otherwise>
<c:forEach items="${users}" var="user" begin="2" end="8" step="2">
<tr>
<td>
<c:out value="${user.username}"/>
</td>
<td>
<c:out value="${user.age}"/>
</td>
<td>
<c:out value="${user.group.name}"/>
</td>
</tr>
</c:forEach>
</c:otherwise>
</c:choose>
</table>
<li>循环控制标签c:forEach,输出map</li><br>
<c:forEach items="${map}" var="v">
${v.key }=${v.value }<br>
</c:forEach>
</body>
</html>
说明:在表格中通常需要隔一行换一个背景,比如偶数行背景为红色,奇数行背景为白色,可采用jstl中的c:foreach来控制。见本例中的:“循环控制标签c:forEach,varstatus”处。

热点排行