JSTL之<c:forEach>

1 篇文章 0 订阅

<c:forEach>例1

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 
<tbody>
<c:forEach var="instance" items="${requestScope.list}" varStatus="status">
	<c:choose>
		<c:when test="${status.index%2==0}" >//斑马线
	   		<tr class="SelcetOff">
	   	</c:when>
		<c:when test="${status.index%2==1}" >
	   		<tr class="SelcetOn">
<pre name="code" class="html">		</c:when>
   	</c:choose>
   		<td><div align="center">${instance.userName}</div></td>
   		<td><div align="center">${instance.address }</div></td>
   		<td><div align="center">${instance.role }</div></td>
<pre name="code" class="html">		</tr>
</c:forEach>
</tbody>

 
 

status属性说明

status.current 当前这次迭代的(集合中的)项

status.index 索引,从0开始,相当于for循环里的i

status.count 计数器,从1开始

status.first 判断是否是第一条

status.last  判断是否是最后一条

status.begin 获得开始时的记录

status.end   获得结束时的记录

status.step  获得步长


<c:forEach>例2

${requestScope.list}是一个List<User>,instance是其中的一个实例

<pre name="code" class="html"><c:forEach items="${requestScope.winMap}" var="entry" varStatus="status" >
				
	<tr>
<pre name="code" class="html"><pre name="code" class="html">		<td><c:out value="${entry.key}" />等奖</td>

 
 
		<td><c:out value="${entry.value.winCount}" /></td><pre name="code" class="html"><pre name="code" class="html"><pre name="code" class="html">		<td><c:out value="${entry.value.winMoney}" /></td>
<pre name="code" class="html">	</tr>
	<c:if test="${!status.last}">
	<tr><pre name="code" class="html"><pre name="code" class="html">		<td>追加</td><td><c:out value="${entry.value.winAddCount}" /></td>
 
		<td><c:out value="${entry.value.winAddMoney}" /></td>
<pre name="code" class="html">	</tr>
	</c:if>
</c:forEach> 
 
 
 
 
 
 
 

后台java代码

${requestScope.winMap}是一个Map<String,Win>,entry是其中的一个实例,${!status.last}表示不是最后一项

Map map=request.getParameterMap();
Iterator ite = map.entrySet().iterator();
while (ite.hasNext()) {
      Map.Entry mapEntry = (Map.Entry)_ite.next();
      request.setAttribute((String)mapEntry.getKey(), mapEntry.getValue());
}

参考内容
JSTL标签用法 详解
http://blog.csdn.net/zwhfyy/archive/2009/07/09/4335200.aspx

迭代
http://www.java2000.net/p3893

<fmt:>标签用法

<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %> <span><span>
<fmt:formatDate value=</span><span class="string">"${date}"</span><span> pattern=</span><span class="string">"yyyy年MM月dd日 HH:mm:ss"</span><span>/></span></span>

这里的value值是你要格式化的值,一般是后台传送到前台的Date对象  后面的pattern对应的是要显示的格式,可以自定义.

<fmt:formatNumber value="${ entry.pretaxprize/100 }" pattern="##.##" minFractionDigits="2" />元
这里的Value值是任何数值类型,pattern为要显示的格式,此处为显示小数点后两位. minFractionDigits="2" 代表要精确的位数.   记住,这里的值都是通过四舍五入得到的,并非完全精确!


JSTL函数

<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> 

函数 描述
fn:contains(string, substring) 如果参数string中包含参数substring,返回true
fn:containsIgnoreCase(string, substring) 如果参数string中包含参数substring(忽略大小写),返回true
fn:endsWith(string, suffix) 如果参数 string 以参数suffix结尾,返回true
fn:escapeXml(string) 将有特殊意义的XML (和HTML)转换为对应的XML character entity code,并返回
fn:indexOf(string, substring) 返回参数substring在参数string中第一次出现的位置
fn:join(array, separator) 将一个给定的数组array用给定的间隔符separator串在一起,组成一个新的字符串并返回。
fn:length(item) 返回参数item中包含元素的数量。参数Item类型是数组、collection或者String。如果是String类型,返回值是 String中的字符数。
fn:replace(string, before, after) 返回一个String对象。用参数after字符串替换参数string中所有出现参数before字符串的地方,并返回替换后的结果
fn:split(string, separator) 返回一个数组,以参数separator 为分割符分割参数string,分割后的每一部分就是数组的一个元素
fn:startsWith(string, prefix) 如果参数string以参数prefix开头,返回true
fn:substring(string, begin, end) 返回参数string部分字符串, 从参数begin开始到参数end位置,包括end位置的字符${fn:substring("ABC","1","2")}截取结果为“ B ”。
fn:substringAfter(string, substring) 返回参数substring在参数string中后面的那一部分字符串
fn:substringBefore(string, substring) 返回参数substring在参数string中前面的那一部分字符串,${fn:substringBefore("ABCD","BC")}截取的结果为“ A ”。
fn:toLowerCase(string) 将参数string所有的字符变为小写,并将其返回
fn:toUpperCase(string) 将参数string所有的字符变为大写,并将其返回
fn:trim(string) 去除参数string 首尾的空格,并将其返回


EL表达式拾遗:
pageScope、requestScope、sessionScope 和applicationScope对应JSP的pageContext、request、session和application

request.getParameter(String name)
request.getParameterValues(String name)

application.getInitParameter("userid")

分别等价于:
${param.name}
${paramValues.name}
${initParam.userid}

${pageContext.request.queryString}         取得请求的参数字符串
${pageContext.request.requestURL}         取得请求的URL,但不包括请求之参数字符串
${pageContext.request.contextPath}         服务的web application 的名称
${pageContext.request.method}           取得HTTP 的方法(GET、POST)
${pageContext.request.protocol}         取得使用的协议(HTTP/1.1、HTTP/1.0)
${pageContext.request.remoteUser}         取得用户名称
${pageContext.request.remoteAddr }         取得用户的IP 地址
${pageContext.session.new}             判断session 是否为新的
${pageContext.session.id}               取得session 的ID
${pageContext.servletContext.serverInfo}   取得主机端的服务信息
${header["User-Agent"]}  要取得用户浏览器的版本

运算符empty,?:

例:${empty param.name}、${A?B:C}、${A*(B+C)}


(本文转自: http://gundumw100.iteye.com/blog/473382)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值