码迷,mamicode.com
首页 > Web开发 > 详细

【JSTL】--JSTL表达式:c:forEach,varstatus/begin end/循环控制标签--drp215

时间:2016-02-15 18:25:55      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

 

jstl_core.jsp

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c"%>
<%@ page import="com.bjpowrnode.jstl.*" %>
<%@ page import="java.util.*" %>

<!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>
	<h3>采用forEach标签,varstatus</h3><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 mod 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>
	
	
	
	 <h3>采用forEach标签,begin ,end</h3>
	 <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>${user.username}</td>
	 					<td>${user.age }</td>
	 					<td>${user.group.name }</td>
	 				</tr>
	 			</c:forEach>
	 		
	 		</c:otherwise>
	 		
	 	</c:choose>
	 </table>
	
	<li>演示循环控制标签:forEach,输出Map</li><br>
	<c:forEach items="${map }" var ="entry">
		${entry.key },${entry.value}<br>
	</c:forEach>
	
	<li>循环控制标签:forTokens</li><br>
	<c:forTokens items=	"${strTokens}" delims="#" var="v">
		${v }<br>
	</c:forTokens>
	
</body>
</html>

 

JstlCoreServlet

/**
 * 
 */
package com.bjpowrnode.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;

/**
 * @ClassName:JstlCoreServlet
 * @Description:TODO
 * @author wm
 * @date 2016年2月15日上午11:40:32
 */
public class JstlCoreServlet extends HttpServlet {

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//普通字符串
		request.setAttribute("hello","Hello World!");
		request.setAttribute("welcome", "<font color=‘red‘>欢迎你来到这个世界!</font>");
		
		//条件控制标签、
		request.setAttribute("v1", 10);
		request.setAttribute("v2", 20);
		
		request.setAttribute("userList", new ArrayList());
		
		
		//结构
		Group group =new Group();
		group.setName("动力节点drp 603班");
		
		List users=new ArrayList();
		for(int i =0;i<10;i++){
			User user=new User();
			user.setUsername("张三_"+i );		
			user.setAge(23+i);
			user.setGroup( group);	
			users.add(user);
		}
		
		request.setAttribute("users", users);
		
		//map
		Map map=new HashMap();
		map.put("k1", "v1");
		map.put("k2", "v2");
		map.put("k3", "v3");
		map.put("k4", "v4");
		request.setAttribute("map", map);
		
		
		//forTokens
		request.setAttribute("strTokens", "1#2#3#4#5");
		
		
		request.getRequestDispatcher("/jstl_core.jsp").forward(request, response);
	}


}

 

【JSTL】--JSTL表达式:c:forEach,varstatus/begin end/循环控制标签--drp215

标签:

原文地址:http://www.cnblogs.com/wangmei/p/5190986.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!