JSTL备忘

来源:互联网 发布:短域名生成器自定义 编辑:程序博客网 时间:2024/06/06 14:29
jstl(jsp标准标签库)学习:
表达式相关标签: 
<c:out> <c:set> <c:remove> <c:catch>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding
="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<c:set value="21" var="num"/>
<c:out value="${num}"/>

<c:set var="num">21</c:set>
<c:out value="${num }"/>
</body>
</html>

<%@ page contentType="text/html;charset=gb2312"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<html>
  
<head>
     
<title>
     
</title>
  
</head>
  
<body>
  
<c:set value="21" var="num"/>
  
  
<c:remove var="num"/>
  
<c:out value="${num }"/>
  
</body>
</html>


流程控制相关标签:
<c:if> <c:choose> <c:when> <c:otherwise>,其中<c:choose>标签是<c:when><c:othwise>的父标签:
                  
<c:choose>
                         
<c:when test="">
                         
</c:when>
                         
<c:when test="">
                         
</c:when>
                         
<c:otherwise>
                         
</c:otherwise>
                   
</c:choose>
                   且
<c:choose></c:choose>中至少要有一个<c:when>标签,而<c:otherwise>标签则可以没有,还有就是<c:when>标签必须放在<c:otherwise>标签的前面,
                 只有当
<c:when>全为假时才会执行<c:otherwise>里面的内容,最后一个就是当有多个<c:when>为真的话,只会执行第一个<c:when>里面的东西;(很像switch语句)

<%@ page language="java" contentType="text/html; charset=gb2312"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<c:set value="电子工业出版社" var="publisher"/>
<c:if test="${publisher=='电子工业出版社'}" var="namecheck">
    检查正确!
</c:if>
<c:out value="${namecheck}"/>

<c:set value="电子工业出版社" var="publisher"/>
<c:choose>
   
<c:when test="${publisher=='电子工业出版社'}">
      电子工业出版社!
   
</c:when>
   
<c:when test="${publisher=='机械工业出版社'}">
         机械工业出版社!
   
</c:when>
   
<c:otherwise>
         本书出版社不明!
   
</c:otherwise>
</c:choose>
</body>
</html>

迭代相关标签: 
<c:forEach> <c:forTokens>

<%@ page language="java" contentType="text/html; charset=gb2312"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
  String name
=new String("姓名=邓佳容|年龄=10个月|性别=女");
  request.setAttribute(
"name",name);
%>

<c:forTokens items="${name}" var="currentName" varStatus="currentVarStatus" delims="|">
   当前元素为:
<c:out value="${currentName}"/>
   当前索引号为:
<c:out value="${currentVarStatus.index}"/>
   当前迭代次数为:
<c:out value="${currentVarStatus.count}"/><hr>
</c:forTokens>

<c:forTokens items="${name}" var="currentName" varStatus="currentVarStatus" delims="|">
   
<c:forTokens items="${currentName}" var="currentName1" varStatus="currentVarStatus" delims="="> //这里相当于两层循环,先用"|"分割,再用"="分割 "
    当前元素为:<c:out value="${currentName1}"/>
    当前索引为:
<c:out value="${currentVarStatus.index}"/>
    当前迭代次数为:
<c:out value="${currentVarStatus.count}"/>
    
</c:forTokens>
</c:forTokens>
</body>
</html>


URL相关标签: 
<c:import><c:param>  <c:redirect> <c:url>

      
<c:import>标签用于把当前JSP页面之外的其他静态或动态文件包含进来,功能类似于JSP的动作指令<jsp:include>,但两者有所差别:<jsp:include>只能包含与当前JSP页面处于同一个Web应用下的文件,而<c:import>标签无此限制,甚至可以是其他网站的文件,在<c:import></c:import>中加入<c:param></c:param>标签的话还可以实现向import的URL传入参数的功能。
      
<c:redirect>是重定向到一个新的URL,也可以实现传入参数的功能;
      
<c:url>是生成一个新的URL,也可以实现传入参数的功能;
 
原创粉丝点击