jstl迭代标签

阅读更多
一 定义

jstl迭代标签_第1张图片
 
 
二 代码
<%@ page language="java" import="java.util.*,com.cakin.domain.*" pageEncoding="utf-8" %>
<%@ taglib prefix="c"  uri="http://java.sun.com/jsp/jstl/core"; %>
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";>
<html>
  <head>   
    <title>My JSP 'c_out.jsp' starting pagetitle>
  head>
  <body>
       <%
        ArrayList a1=new ArrayList();
                Rat rat=new Rat();
                rat.setName("小宝");
                rat.setAge(2);
                Rat rat1=new Rat();
                rat1.setName("大宝");
                rat1.setAge(4);
                Rat rat2=new Rat();
                rat2.setName("老宝");
                rat2.setAge(8);
                a1.add(rat);
                a1.add(rat1);
                a1.add(rat2);
                request.setAttribute("all",a1);
                request.setAttribute("abc","12;34;你好;haoma");
                %>
                <h1>传统迭代h1>
                <%
                ArrayList a12=(ArrayList)request.getAttribute("all");
                for(Rat rat3:a12)
                {
                        out.println(rat3.getName()+" "+rat3.getAge());
                }
                %>
                <h1>迭代标签c:foreachh1>
                <c:forEach items="${all}" var="rat">
                        <c:out value="${rat.name}">c:out>
                        <c:out value="${rat.age}">c:out>
                c:forEach>
                <h1>第二种迭代 1 to 10h1>
                <c:forEach var="i" begin="1" end ="10">
                        <c:out value="${i}">c:out>
                c:forEach>
                <h1>第三种迭代 1 to 10 step=3h1>
                <c:forEach var="i" begin="1" end ="10" step="3">
                        <c:out value="${i}">c:out>
                c:forEach>
                <h1>fortokensh1>
                <c:forTokens items="12;34;你好" delims=";" var="temp">
                ${temp}
                c:forTokens>
                <br>
                <c:forTokens items="${abc}" delims=";" var="temp">
                ${temp}
                c:forTokens>
  body>
html>
 
三 测试结果

 


jstl迭代标签_第2张图片
 
  • jstl迭代标签_第3张图片
  • 大小: 71.3 KB
  • jstl迭代标签_第4张图片
  • 大小: 53.5 KB
  • 查看图片附件

你可能感兴趣的:(jstl,迭代标签)