jstl举例

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="user.User"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
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 'index.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>
  <%
  	request.setAttribute("hello","hello world");
  	request.setAttribute("bj","<font color='red'>北京</font>");
  	request.setAttribute("v1",1);
  	request.setAttribute("v2",2);
  	request.setAttribute("v3",new ArrayList());
  	request.setAttribute("v4","test");
  	//测试c:forEah
  	List<User> users=new ArrayList<User>();
  	for(int i=0;i<10;i++){
  		User user=new User();
  		user.setUsername("u_"+i);
  		user.setAge(i);
  		users.add(user);
  	}
  	request.setAttribute("users",users);
  	//测试map
  	Map map=new HashMap();
  	map.put("k1","v1");
  	map.put("k2","v2");
  	request.setAttribute("map",map);
  	
  	//测试c:forTokens
  	request.setAttribute("tokens","1,2,3,4,5,6");
  %>
  <body>
  	<li>c:out测试</li>
    c:out :<c:out value="${hello}"></c:out><br>
    
    bj:<c:out value="${bj}" escapeXml="false"></c:out>
    
    <li>c:set和c:remove测试</li>
    <c:set value="123" var="temp" ></c:set>
    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 }<br>
    </c:if>
    <c:if test="${empty v3}">
    	v3为空<br>
    </c:if>
    <c:if test="${empty v4}">
    	v4为空<br>
    </c:if>
    <p>
    <li>c:choose,c:when,c:otherwise测试</li>
    <c:choose>
    	<c:when test="${v1 lt v2}">
    		v1小于v2<br>
    	</c:when>
    	<c:otherwise>
    		v1大于v2<br>
    	</c:otherwise>
    </c:choose>
    <p>
    <li>c:forEach测试</li><br>
    <table border="1">
    	<tr>
    		<td>姓名</td>
    		<td>年龄</td>
    	</tr>
    	<c:choose>
    		<c:when test="${empty users}">
    			<tr>
    				<td colspan="2">没有符合条件的记录</td>
    			</tr>
    		</c:when>
    		<c:otherwise>
    			<c:forEach items="${users}" var="u" varStatus="vs">
    				<c:choose>
    					<c:when test="${vs.count%2==0}">
    						<tr bgcolor="red">
    					</c:when>
    					<c:otherwise>
    						<tr bgcolor="blue">
    					</c:otherwise>
    				</c:choose>
    					<td>${u.username }</td>
    					<td>${u.age }</td>
    				</tr>
    			</c:forEach>
    		</c:otherwise>
    	</c:choose>
    </table>
    <li>c:forEach,begin,end,step测试</li>
    <table border="1">
    	<tr>
    		<td>姓名</td>
    		<td>年龄</td>
    	</tr>
    	<c:choose>
    		<c:when test="${empty users}">
    			<tr>
    				<td colspan="2">没有符合条件的记录</td>
    			</tr>
    		</c:when>
    		<c:otherwise>
    			<c:forEach items="${users}" var="u" begin="2" end="8" step="2">
    				<tr>
    					<td>${u.username }</td>
    					<td>${u.age }</td>
    				</tr>
    			</c:forEach>
    		</c:otherwise>
    	</c:choose>
    </table>
    <p>
    <li>普通循环</li>
    <c:forEach begin="1" end="2">
    	a<br>
    </c:forEach>
    
    <p>
    <li>循环输出map</li>
    <c:forEach items="${map}" var="v">
    	${v.key }:${v.value }<br>
    </c:forEach>
    
    <p>
    <li>c:forTokens测试</li>
    <c:forTokens items="${tokens}" delims="," var="t">
    	${t }<br>
    </c:forTokens>
    
    <p>
    <li>c:catch测试</li>
    <c:catch var="e">
    	<%Integer.parseInt("s"); %>
    </c:catch>
    ${e }<br>
    
    <p>
    <li>c:import测试</li>
    <c:import url="http://163.com"></c:import>
    
    <p>
    <li>c:url,c:param测试</li>
    <c:url value="http://www.163.com" var="v">
    	<c:param name="username" value="jack"></c:param>
    	<c:param name="age" value="20"></c:param>
    </c:url>
    ${v }<br>
    
    <p>
    <li>c:redirect测试</li>
    <c:redirect url="http://www.163.com"></c:redirect>
  </body>
  
</html>

你可能感兴趣的:(html,C++,c,jsp,C#)