jsp-c标签

王昭廷笔记		
*****六、JSTL中的核心标签库(替换掉JSP中的Java脚本)
	c:if
		作用:判断是否为true,如果为true,那么标签的主体内容就会显示。
		属性:
			test:必须的。要求必须是boolean的。支持表达式(EL或Java表达式)
			var:保存test运算结果的变量
			scope: 保存的域范围。默认是page
			
	c:forEach
		遍历:数组、List、Set、Map
		属性:
			items:要遍历的目标对象。支持表达式
			var:变量名。指向当前遍历的集合中的一个元素
			begin:开始的索引(含)
			end:结束的索引(含)
			step:步长。默认是1
			varStatus:取一个名字,引用了一个对象。
				该对象有以下方法:
					int getIndex():当前记录的索引号。从0开始
					int getCount():当前记录的顺序。从1开始
					boolean isFirst():是否是第一条记录
					boolean isLast():是否是最后一条记录
					
					
			
	----------------------------------------------------------------------
代码演示
  <body>
    <%
String str[] = {"a","b","c"};
    pageContext.setAttribute("str",str);
    %>
    <c:forEach items="${str}" var="s">
    	${s}<br/>
    </c:forEach>
    <hr/>
    <%
List list = new ArrayList();
    list.add("aa");
    list.add("bb");
    list.add("cc");
    pageContext.setAttribute("list",list);
    %>
   	<c:forEach items="${list}" var="s">
   		${s}<br/>
	</c:forEach>
	<hr/>
	<%
Set set = new HashSet();
	set.add("aaa");
	set.add("bbb");
	set.add("ccc");
	pageContext.setAttribute("set",set);
	%>
	<c:forEach items="${set}" var="s">
		${s }<br/>
	</c:forEach>
	<hr/>
	<%
Map map = new LinkedHashMap();
	map.put("a","aaaa");
	map.put("b","bbbb");
	map.put("c","cccc");
	pageContext.setAttribute("map",map);
	%>
<!-- var指向的类型是Map.Entry类型的 -->
	<c:forEach items="${map}" var="me">
		${me.key}=${me.value}<br/>
	</c:forEach>
	<hr/>
	<%
String s1[] = {"a","b","c","d","e","f"};
	pageContext.setAttribute("s1",s1);
	%>
	<c:forEach items="${s1}" var="s" begin="1" end="4" step="2">
		${s}<br/>
	</c:forEach>
	<hr/>
	<c:forEach items="${s1}" var="s" step="2">
		${s}<br/>
	</c:forEach>
	<hr/>
	<c:forEach begin="1" end="100" var="s">
		${s}
	</c:forEach>
	<hr/>

-------------表格
	<%
	List<Person> ps = new ArrayList<Person>();
	ps.add(new Person("葛付以","1",true));
	ps.add(new Person("余睿","1",false));
	ps.add(new Person("朱巧玲","0",false));
	ps.add(new Person("蒋小平","0",false));
	ps.add(new Person("邹海洋","1",true));
	ps.add(new Person("董泽坤","1",false));
	ps.add(new Person("梁胜海","1",false));
	pageContext.setAttribute("ps",ps);
	%>
	<c:forEach items="${ps}" var="p">
--隔行配色--		${p.username}:${p.gender=="1"?"男":"女"}:${p.married?"已婚":"未婚"}<br/>
	</c:forEach>
	<hr/>
	
	<table border="1" width="60%">
		<tr>
			<th>索引</th>
			<th>顺序</th>
			<th>第一个</th>
			<th>最后一个</th>
			<th>姓名</th>
			<th>性别</th>
			<th>婚姻</th>
		</tr>
		<c:forEach items="${ps}" var="p" varStatus="vs">
			<tr>
				<td>${vs.index}</td>
				<td>${vs.count}</td>
				<td>${vs.first }</td>
				<td>${vs.last }</td>
				<td>${p.name}</td>
				<td>${p.gender=="1"?"男":"女"}</td>
				<td>${p.married?"已婚":"未婚"}</td>
			</tr>
		</c:forEach>
	</table>
	<hr/>
	<table border="1" width="60%">
		<tr>
			<th>顺序</th>
			<th>姓名</th>
			<th>性别</th>
			<th>婚姻</th>
		</tr>
		<c:forEach items="${ps}" var="p" varStatus="vs">
			<tr bgcolor="${vs.index%2==0?'#CFCFCF':'#4D88AB'}">
				<td>${vs.count}</td>
				<td>${p.name}</td>
				<td>${p.gender=="1"?"男":"女"}</td>
				<td>${p.married?"已婚":"未婚"}</td>
			</tr>
		</c:forEach>
	</table>
  </body>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值