9、EL和JSTL介绍及使用

什么是EL

Expression Language表达式语言,EL和JSTL一起使用取代JSP页面中嵌入java代码写法。

  • EL功能

    • 获取数据
    • 执行运算
    • 获取web开发常用对象
  • EL写法
    ${ };

EL——获取数据

EL表达式语句在执行的时候,会调用pageContext.findAttribute()方法。分别从page、request、session、application范围查找相应对象,找到就会返回相应对象,找不到返回“”(不是null,是空的字符串),EL获取数据需要在四个作用范围中。

<% pageContext.setAttribute("name","张三");
	request.setAttribte("name","李四");
	session.setAttribte("name","王五");
	application.setAttribte("name","赵六");
%>
${pageScope.name};
${requestScope.name};
${sessionScope.name};
${applicationScope.name};
<h3>EL的简写</h3>
${ name }    <!-- 底层调用pageContext.findAttribute()-->

EL获取数组和集合数据:

<h3>EL获取数组数据</h3>
<% String[] arrs = {"aa","bb","cc"};
	pageContext.setAttribute("arrs",arrs);
%>
${ arrrs[0] }
${ arrrs[1] }
${ arrrs[2] }

<h3>EL获取List集合数据</h3>
<% List<String> list = new ArrayList<String>();
	list.add("aa");
	list.add("bb");
	list.add("cc");
	pageContext.setAttribute("list",list);
%>
${ list[0] }
${ list[1] }
${ list[2] }

<h3>EL获取Map集合数据</h3>
<% Map<String,Integer> map = new HashMap<String,Integer>();
	map.put("111",aaa);
	map.put("222",bbb);
	map.put("333.444",ccc);
	pageContext.setAttribute("map ",map);
%>
${ map.111 }
${ map.222 }
${ map["333.444"] }

EL——执行运算

<%pageContext.setAttribute("n1","10");
	pageContext.setAttribute("n1","10");
%>
<h3>执行算数运算</h3>
${ n1+n2 }
<h3>执行关系运算,返回true or false</h3>
${ n1 < n2} -- ${n1 lt n2} <br />
${ n1 > n2} -- ${n1 gt n2} <br />
${ n1 == n2} -- ${n1 eq n2} <br />
${ n1 >= n2} -- ${n1 ge n2} <br />
${ n1 <= n2} -- ${n1 le n2} <br />
${ n1 != n2} -- ${n1 ne n2} <br />

<%pageContext.setAttribute("n3","30");
	pageContext.setAttribute("n4","40");
%>
<h3>执行逻辑运算</h3>
${(n1 < n2) && (n3 < n4)} -- ${(n1 < n2) and (n3 < n4)}<br/>
${(n1 < n2) || (n3 < n4)} -- ${(n1 < n2) or (n3 < n4)}<br/>
${!(n1 < n2)} -- ${not(n1 < n2)}<br/>
<h3>执行三元运算</h3>
${n1 < n2? "n1小于n2":"n1不小于n2"}
<h3>空运算符</h3>
${empty n1}

EL——获取web开发常用对象

EL表达式定义了11个web开发常用对象。使用这些对象可以很方便的获取web开发中的一些常见对象,并可以读取这些对象中的数据。

在这里插入图片描述
在这里插入图片描述

什么是JSTL

JSP StandardTag Library,JSP标准标签库。

  • 主要和EL一起取代传统页面上直接嵌入java代码的写法,提升程序可读性、维护性和方便性。
  • 使用需要引入jstl.jar以及standard.jar两个jar包。
  • JSP页面引入标签库(固定写法,C:核心标签库)
    在这里插入图片描述
<% pageContext.setAttribute("name","aaa");
%>
<c:set value="aaa" var="name" scope="page"></c:set>
${ name }

JSTL——if标签

  • if标签的使用
<c:set value="10" var="i" scope="page"></c:set>
<c:if test="${ i >= 10}">
	<font color="red">i大于等于10</font>
</c:if>
<c:if var="flag" test="${ i < 10}" scope="page">
	<font color="blue">i小于10</font>
</c:if>
<c:if test="${ flag }">
	flag为true
</c:if>
  • if标签的属性

    • test属性:条件
    • var属性:将test中的条件的值赋给一个变量,在var中定义变量
    • scope属性:作用范围

JSTL——foreach标签

  • foreach标签的使用
<h3>遍历数组</h3>
<% String[] arrs = {"aa","bb","cc"};
	pageContext.setAttribute("arrs",arrs);
%>
<c:forEach var="s" items="${ arrs }">
	${ s }
</c:forEach>

<h3>遍历List集合</h3>
<% List<String> list = new ArrayList<String>();
	list.add("aa");
	list.add("bb");
	list.add("cc");
	pageContext.setAttribute("list",list);
%>
<c:forEach var="s" items="${ list }">
	${ s }
</c:forEach>

<h3>遍历Map集合</h3>
<% Map<String,Integer> map = new HashMap<String,Integer>();
	map.put("111",aaa);
	map.put("222",bbb);
	map.put("333",ccc);
	pageContext.setAttribute("map ",map);
%>
<c:forEach var="entry" items="${ map }">
	${ entry.key } -- ${ entry.value }
</c:forEach>

<h3>遍历从110</h3>
<c:forEach var="i" begin="1" end="10" step="1">
	${ i }
</c:forEach>

<h3>遍历100200,每次加2,到第三个数的时候将该数字变蓝</h3>
<c:forEach var="i" begin="100" end="200" step="2" varStatus="status">
	<c:if test="${ status.count % 3 == 0 }">
		<font color="blue">${ i }</font>
	</c:if>
	<c:if test="${ status.count % 3 != 0 }">
		${ i }
	</c:if>
</c:forEach>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章