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>
</tr>
<c:when test="${empty userlist}">
<tr>
<td>没有符合条件的数据</td>
</tr>
</c:when>
<c:otherwise>
<c:forEach items="${userlist}" 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>

<li>测试循环控制标签c:forEach,普通循环</li>
<c:forEach begin="1" end="10">
a<br>
</c:forEach>

<li>测试循环控制标签c:forEach,输出map</li>
<c:forEach items="${maplist}" var="v">
${v.key }-----${v.value }<br>
</c:forEach>

<li>测试c:catch</li>
<%
try{
Integer.parseInt("dsafd");
}catch(Exception e){
out.println(e.getMessage());
}
%>
<c:catch var="info">
<%
Integer.parseInt("dsafd");
%>
</c:catch>
${info }<br>

<li>测试c:import</li>
相当于include用法<br>
<c:import url="hb.html"/>
<c:import url="http://localhoust:8080/struts_login"/>

<li>测试c:url 和 c:param</li>
<c:url value="http://localhost:8080/drp/user_add.jsp" value="v">
<c:param name="username" value="jack"/>
<c:param name="age" value="20"></c:param>
</c:url>
${v}<br>

<li>重定向c:redirect</li>
<c:redirect context="/struts_login" url="/index.jsp"></c:redirect>

</body>
</html>

posted @ 2011-10-19 21:58  胖鹅  阅读(241)  评论(1编辑  收藏  举报