JSTL-core核心标签库源代码及讲解

来源:互联网 发布:天天特价淘宝网官网 编辑:程序博客网 时间:2024/06/06 09:32
Core 标签库
一、通用标签:
1、<c:out>标签
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">


<title>My JSP 'out.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>
<!-- c:out用于是输出的标签 -->
<c:out value="无主体标签的输出" />
<br>
<br>
<!-- 当value为null时,默认值.. -->
<c:out value="${310null}">
<!-- 下面输出的是default属性值 -->
  value值为null时输出。
  </c:out>
<br />
<!-- escapeXml属性为转换字符实体的属性,默认值为true -->
<c:out value="<hr>原样输出的HTML标签<hr>" escapeXml="true" />
<br />
<c:out value="<hr>转换输出的HTML标签<hr>" escapeXml="false" />
</body>
</html>
2、<c:set>标签:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!-- 先引入核心标签 -->
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
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 'set.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>
<!-- c:set用于设置JSP页面的各种域范围中的变量 -->
<!-- var属性不支持EL表达式,属于String类型,作用是: 标识属性值的变量名-->
<!-- scope属性不支持EL表达式,属于String类型,作用是:限制变量的作用域,默认为page -->
<c:set var="bodyc" scope="session">
body content
   </c:set>
输出有标签体变量:
<c:out value="${bodyc}" />
<br>
<c:set var="username" value="bzc" />
输出无标签体变量:
<c:out value="${username}" />
<br>
<!-- 因为我们没有创建UserBean类所以这里引入的类是一个错误的类在执行的时候会报错。 -->
org.omg.CosNaming._BindingIteratorStub userbean = new
org.omg.CosNaming._BindingIteratorStub();
<!-- target属性支持EL表达式,属于object类型,作用是:要设置属性的对象,对象必须是JavaBean对象或Map对象 -->
<!--<c:set value="bzc" target="${userbean}" property="username" />
无标签体-输出设置bean中属性name的值:
<c:out value="${userbean.username }"></c:out>
<br>
<c:set target="${userbean }" property="username">
   bianchengzhi
   </c:set>
有标签体-输出设置bean中属性name的值
<c:out value="${userbean.username }"></c:out>
-->
<%
Map map = new HashMap();
request.setAttribute("map", map);
%>
<!-- target指定property设置的值是JavaBean还是Map集合-->
<!-- Map集合有键值对 所以property指定键 value指定值 -->
<!-- 用set标签向集合中插入值 -->
<c:set value="唐僧" property="aaa" target="${map}">
<!-- 用EL表达式来显示是否插入成功 -->
</c:set>
${map.aaa}
</body>
</html>
3、<c:remove>、<c:catch>标签:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
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 'remove.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>
<!-- remove为删除标签,它有两个属性分别是var,scope。var是提供要删除的变量名,scope是在jsp页面中的范围,默认为page -->
<c:set var="username" value="abc" />
输出无标签体变量:
<c:out value="${username }" />
<br>
<c:remove var="username" />
执行remove之后的变量
<c:out value="${username}">值为NULL,执行此处</c:out>
<br>
<br>
-----------------------------c:catch---------------------------------------
<br>
<!-- catch标签是为了抛出异常的。 -->
<c:catch var="exception">
<%
int[] nums = new int[5];
System.out.println(nums[5]);
%>
</c:catch>
输出异常:
<c:out value="${exception}"></c:out>
<br>
异常 exception.message:
<c:out value="${exception.message}"></c:out>
<br>
异常 exception.cause:
<c:out value="${exception.cause}"></c:out>
<br>
异常 exception.stackTrace:
<c:out value="${exception.stackTrace}"></c:out>
</body>
</html>
二、条件标签体:(<c:if>、<c:choose>、<c:when>与<c:otherwise>)
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
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 'remove.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>
----------------------------c:if----------------------------------------
<br>
<!-- 此标签用于条件输出 类似于jsp中的<%if (true || false) {
}%>-->
<c:set var="username" value="0" />
<!-- test属性为判断条件 -->
<c:if test="${username!=1 }">
   条件成立,执行此处
   </c:if>
<br>
--------------------c:choose、c:when、c:otherwise-----------------------
<br>
<!-- c:choose是作为c:when和c:otherwise的父标签使用的除了空白字符外,
   c:choose的标签体只能包含这两个标签。c:choose标签没有任何属性,  
    在它的标签体内只能嵌套一个或多个c:when标签和0个或1个c:otherwise标签,
     并且所有的c:when标签必须出现在同一个c:choose标签的c:otherwise子标签之前。
   c:when作为c:choose的子标签,c:when有一个test属性,该属性的值为布尔型,如果
   test的值为true,则执行c:when标签体的内容。
   c:otherwise标签没有属性,它必须作为c:choose标签的最后分支出现。
   -->
<c:set var="score" value="99" />
<!-- 可以把此部分用if-else来替换 -->
<c:choose>
<c:when test="${score>90}">
  成绩优秀
  </c:when>
<c:when test="${score>80&&score<90}">
  成绩优良
  </c:when>
<c:when test="${score>70&&score<80}">
  成绩一般
  </c:when>
<c:otherwise>成绩较差!</c:otherwise>
</c:choose>
</body>
</html>




原创粉丝点击