JSTL标签

2 篇文章 0 订阅

JSP 标准标签库(JSTL)

JSP标准标签库(JSTL)是一个JSP标签集合,它封装了JSP应用的通用核心功能。

它要和EL表达式联合使用,什么是EL表达式,请看上一篇博客

如何使用JSTL?

1、首先要引入外部jar包,

2、在jsp最上面引入taglib,如下代码

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

3、开始使用(演示的代码片段为:servlet服务器端代码、jsp客户端代码、浏览器显示效果)

1、c:out。这个就是输出功能。

//简单字符串
request.setAttribute("hello", "HelloWorld");
//如果有hello这个属性就输出,没有默认输出default的值
<c:out value="${hello }" default="没有值" />

2、c:set、c:remove。定义变量和移除变量

<c:set value="root" var="userid" />
	userid:${userid }
	<c:remove var="userid" />
	userid:${userid }

3、c:if,但是这个没有else

            //条件控制标签
		request.setAttribute("v1", 10);
		request.setAttribute("v2", 20);
		
<li>c:if</li>
	<c:if test="${v1 < v2 }">
		v1小于v2
	</c:if>

4、c:choose c:when c:otherwise.这三个标签我感觉就相当于if、else

        <c:choose>
		<c:when test="${v1>v2 }">
			v1大于v2
		</c:when>
		<c:otherwise>
			v1小于v2
		</c:otherwise>
	</c:choose>

5、c:forEach。循环遍历

            //结构
		Group group=new Group();
		group.setName("高等数学一班");
		List users=new ArrayList();
		for(int i=0;i<10;i++){
			User user=new User();
			user.setUserName("张三_"+i);
			user.setAge(18+i);
			user.setGroup(group);
			users.add(user);
		}
		request.setAttribute("users", users);
 <h3>采用foreach标签</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">
   					<tr>
   						<td>${user.userName }</td>
   						<td>${user.age }</td>
   						<td>${user.group.name }</td>
   					</tr>
   				</c:forEach>
   			</c:otherwise>
   		</c:choose>
   	</table>

      如果让偶数行变色,需要加入varStatus属性,这个可以记录行数

	<h3>采用foreach标签,varstatus</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" 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>

   加入begin、end、step显示从几行开始到几行结束步长为多少

	 <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>

    还可以遍历map(map是没有顺序的,每一次输出都是随机的)

                //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);  
 	<li>循环控制标签forEach,输出map</li>
   	<c:forEach items="${map }" var="entry">
   		${entry.key },${entry.value }
   	</c:forEach>

6、c:forTokens,相当于C#中的split,可以分割字符串

            //forTokens
		request.setAttribute("strTokens", "1#2#3#4#5");
<c:forTokens items="${ strTokens}" delims="#" var="v">
	 ${v }<br>
	</c:forTokens>

评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值