日期:2014-05-16  浏览次数:20399 次

JSTL学习笔记
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib prefix="c" uri="c.tld资源文件中有定义" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'JSTL_test.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <h1>测试jstl核心库</h1>
    <hr>
    <li>测试c:out</li>
    hello(default):<c:out value="${hello}"/><br>
    hello(el表达式):${hello }<br>
    hello(如果不存在可以使用默认值huangbiao):<c:out value="${hello}" default="huangbiao"></c:out>
    hello(如果不存在可以使用默认值huangbiao):<c:out value="${hello}">huangbiao</c:out>
    
    <li>测试c:set和c:remove</li>
    <c:set value="123" var="temp"/>
    temp:${temp }<br>
    <c:remove var="temp"/>
    temp:${temp }<br>
    
    <li>测试条件控制标签c:if</li>
    <c:if test="${v1 lt v2}" var="v">
    `	v1 小于 v2<br>
    	v = ${v }
    </c:if>
    
    <c:if test="${empty v3}">
    	v3为空
    </c:if>
    <c:if test="${not empty v4}">
    	v4不为空
    </c:if>
    
    <li>测试条件控制的标签c:choose,c:when,c:otherwise</li>
    <c:choose>
    	<c:when test="${v1 lt v2}">
    		v1 小于 v2
    	</c:when>
    	<c:when test="${v1 eq v2}">
    		v1 等于 v2
    	</c:when>
    	<c:otherwise>
    		不知道
    	</c:otherwise>
    </c:choose>
 
<li>测试循环控制标签c:forEach</li>
    <table>
    	<tr>
    		<td>姓名</td>
    		<td>年龄</td>
    		<td>所属组</td>
    	</tr>
    	<c:choose>
    		<c:when test="${empty userlist}">
    			<tr>
    				<td>没有符合条件的数据</td>
    			</tr>
    		</c:when>
    		<c:otherwise>
    			<c:forEach item="${userlist}" var="user">
    				<tr>
    					<td>${user.username }</td>
			    		<td><${user.age }/td>
			    		<td>${user.group.name }</td>
    				</tr>
    			</c:forEach>
    		</c:otherwise>
    	</c:choose>
    </table>
    
    <li>测试循环控制标签c:forEach,varstatus</li>
    <table border="1">
    	<tr>
    		<td>姓名</td>
    		<td>年龄</td>
    		<td>所属组</td>
    	</tr>
    	<c:choose>
    		<c:when test="${empty userlist}">
    			<tr>
    				<td>没有符合条件的数据</td>
    			</tr>
    		</c:when>
    		<c:otherwise>
    			<c:forEach items="${userlist}" var="user" varStatus="vs">
    				<c:choose>
    					<c:when test="${vs.count % 2 == 0}">
    						<tr bgcolor="red">
    					</c:when>
    					<c:otherwise>
    						<tr>
    					</c:otherwise>
	    					<td>${user.username }</td>
				    		<td><${user.age }/td>
				    		<td>${user.group.name }</td>
	    				</tr>
    				</c:choose>
    			</c:forEach>
    		</c:otherwise>
    	</c:choose>
    </table>
    
    
 <li>测试循环控制标签c:forEach,begin,end,step</li>
    <table border="1">
    	<tr>
    		<td>姓名</td>
    		<td>年龄</td>
    		<td>所属组</td