EL表达式及JSTL标签库

版权声明:转载请联系作者本人!!! https://blog.csdn.net/weixin_42061805/article/details/81879282

EL表达式:

什么是EL表达式?

EL是jsp内置的表达式语言,从jsp2.0开始,不让使用jsp java脚本,而是使用EL表达式和动态标签来替代java脚本,注意的是:EL代替的只是<%= ... %>

使用EL表达式读取四大域

<!-- 若属性不存在,则输出空字符串,而不是null -->
${pageScope.属性名称}
${request.属性名称}
${sessionScope.属性名称}
${applicationScope.属性名称}

EL的11个内置对象

pageScope:

request:

sessionScope:

applicationScope:以上包括这个为四大域,具体使用几乎相同

param:是一个Map,获取参数的一个值,适用于单值的参数

paramValues:是一个Map,获取参数的值,适用于多值的参数

header:是一个Map,对应请求头,适用于单值的请求头

headerValues:是一个Map,对应请求头,适用于多值的请求头

initParam:获取<content-param>中的参数值

cookie:Map<String, Cookie>,获取cookie的值,返回值是个Cookie类型。例:${cookie.username.value}

pageContext:PageContext类型,例:${pageContext.request.contextPath}

EL函数库

首先需要用jsp的taglib指令导入标签库

<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

函数库方法:

JSTL标签库:

什么是JSTL?

JSLT是apache对EL表达式的扩展,即JSTL依赖BL,并且JSTL是标签语言,使用方便。它与jsp动作标签相同,但是需要我们自己导包。

JSTL四大标签库

core:核心标签库

fmt:格式化标签库

sql:数据库标签库,过时

xml:xml标签库,过时

导入标签库

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

标签内容

core:out、set、remove、url、if、choose、forEach、when等。

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%
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>
  
  <body>
  <%
  	request.setAttribute("text1", "<script>alert('xixi');</script>");
  %>
  ${text} 
  <c:out value="${text} " escapeXml="true" default="default"/>
  <c:set var="a" value="setV" scope="session" />
  <c:out value="${a}" />
  
  <!-- 在session域中移除属性 -->
  <c:remove var="a" scope="session"/><br>
  <c:out value="${a}" default="none" /><br>
  
  <!-- 自动补全项目名称 -->
  <c:url value="/index/jsp" var="url" scope="request"><!-- 一旦给设置了var属性,就不会输出 -->
  	<c:param name="username" value="田清波"></c:param><!-- 自动进行url编码 -->
  </c:url>
  <c:out value="${url }"></c:out>
  <br>
  <a href="${pageContext.request.contextPath }/index.jsp">链接</a><br>
  
  <!-- 相当于if语句 -->
  <c:set var="a" value="if..."></c:set>
  <c:if test="${ not empty a }">
  	<c:out value="${a }"/>
  </c:if><br>
  
  <!-- 相当于if-else if -else语句 -->
  <c:set var="score" value="50"></c:set>
  <c:choose>
  	<c:when test="${score > 100 || score < 0 }">
  		<c:out value="成绩输入错误"></c:out>
  	</c:when>
  	<c:when test="${score >= 90 && score <= 100 }">
  		<c:out value="成绩优秀"></c:out>
  	</c:when>
  	<c:when test="${score >= 80 && score < 90 }">
  		<c:out value="成绩良好"></c:out>
  	</c:when>
  	<c:otherwise>
  		<c:out value="成绩较差"></c:out>
  	</c:otherwise>
  </c:choose><br>
  
  <!-- foreach遍历数组或者集合 -->
  <%
  	String[] strs = {"one","two","three"};
  	request.setAttribute("strs", strs);
  %>
  <c:forEach items="${strs }" var="str">
  	${str }
  </c:forEach><br>
  
  <!-- foreach的循环状态变量 -->
  <c:forEach items="${strs }" varStatus="vs">
  	${vs.count }<!-- 当前循环次数 --><br>
  	${vs.first }<!-- 当前循环是否是第一次 --><br>
  	${vs.last }<!-- 当前循环是否是最后一次 --><br>
  	${vs.index }<!-- 当前循环元素下标 --><br>
  	${vs.current }<!-- 当前元素--><br>
  </c:forEach>
  </body>
</html>

fmt:formatDate、formatNumber

<body>
	<!-- 格式化时间 -->
	<%
		Date date = new Date();
		request.setAttribute("date", date);
	%>
	<fmt:formatDate value="${requestScope.date }" pattern="yy-MM-dd HH:mm:ss"/>
	<br>
	
	<!-- 格式化数字 -->
	<%
		request.setAttribute("num", 3.1415);
	%>
	<!-- 格式为0.000时自动四舍五入 位数不够自动补零 -->
	<fmt:formatNumber value="${requestScope.num }" pattern="0.000"></fmt:formatNumber>
	<br>
	<!-- 格式为#.###时自动四舍五入 位数不够不补零 -->
	<fmt:formatNumber value="${requestScope.num }" pattern="#.#####"></fmt:formatNumber>
</body>

猜你喜欢

转载自blog.csdn.net/weixin_42061805/article/details/81879282