el表达式的语法_JSP中EL和JSTL的使用介绍

6b8c13944af01550ab628567fdf5cb00.png

一、EL(Expression Language)

EL是JSP中的表达式语言

语法:${表达式}

注:具体使用请参考示例代码:
<%@ page import="test_el_jstl.Address"%>
<%@ page import="test_el_jstl.User"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page isELIgnored="false" %>
<%
	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>
</head>
<body>


	<%
		int a = 10;

		//数据在四大作用域对象中
		pageContext.setAttribute("a", a);
		request.setAttribute("aaa", true);
		session.setAttribute("age", 18);
		application.setAttribute("name", "laowang");

		// 简单对象
		User user = new User(1, "admin", "admin", "老王");
		request.setAttribute("uuu", user);
		// 复合对象
		User u2 = new User(2, "admin-laowang", "123123", "老王", new Address(
				"广东省", "广州市", "天河区"));
		pageContext.setAttribute("u2", u2);
		//List中的数据
		List list = new ArrayList();
		list.add("apple");
		list.add("orange");
		list.add("watermelon");
		list.add("pineapple");
		pageContext.setAttribute("fruits", list);
		//Map中的数据
		Map map = new HashMap();
		map.put("name", "zhangsan");
		map.put("age", 18);
		map.put("gender", "女");
		map.put("address", new Address("广东省", "广州市", "天河区"));
		pageContext.setAttribute("person1", map);

		//作用域中有相同的key
		pageContext.setAttribute("xxx", "pageContext");
		request.setAttribute("xxx", "request");
		session.setAttribute("xxx", "session");
		application.setAttribute("xxx", "application");
		
		User uu1 = new User(5,"admin","admin","张三");
		User uu2 = new User(5,"admin","admin","张三");
		
		request.setAttribute("uu1", uu1);
		request.setAttribute("uu2", uu2);
		
		request.setAttribute("score", 99);
		
		pageContext.setAttribute("a1", null);
		pageContext.setAttribute("a2", new ArrayList());
		pageContext.setAttribute("a3", new HashMap());
		pageContext.setAttribute("a4", "");
	%>

	尚学堂教育系统,欢迎${user.realname}光临本站!

	<h3>EL表达式从哪里取值?一般情况下,从作用域对象中取值</h3>
	${a} --- ${aaa} --- ${age} --- ${name}
	<h3>EL表达式可以取什么类型的值?基本数据类型、引用数据类型(对象、复合对象、List、Map)</h3>
	${uuu.realname} --- ${u2.addr.town} --- ${fruits[2]} ---
	${person1.address.city}
	<h3>EL表达式从哪个作用域中取值?默认从最小作用域中开始寻找,找不到往大的作用域中找,都找不到显示为空</h3>
	${xxx}
	<h3>EL从指定作用域中取值?
		pageScope、requestScope、sessionScope、applicationScope</h3>
	${pageScope.xxx }--- ${requestScope.xxx} --- ${sessionScope.xxx } ---
	${applicationScope.xxx }
	<h3>EL表达式还可以取请求参数中的值:param、paramValues</h3>
	${param.username} --- ${paramValues.fav[2] }
	<h3>EL表达式除了“.”操作符之外,还有“[ ]”操作符:“[]”可以解析子EL表达式</h3>
	${uuu["realname"]} --- ${param.type} --- ${uuu[param.type]}
	<h3>EL表达式中的为空判断:empty判断为空的有:空字符串、null、空集合、空map</h3>
	${empty a1} --- ${empty a2} --- ${empty a3} --- ${empty a4}
	<h3>EL表达式可以进行一些基本的数学运算</h3>
	${5+4} --- ${5-4} --- ${5*4} --- ${5/4} --- ${5%4} --- ${5+"4"}
	<h3>EL表达式可以进行一些逻辑判断</h3>
	${5>4} --- ${ 5 == 4} --- ${ 5 eq 4 } --- ${uu1 == uu2} --- ${uu1 eq uu2} --- ${score == 99 }	
</body>
</html>

二、JSTL(JSP Standard Tag Library )

JSTL是JSP中的标准库

使用:【1】.下载jar包,并导入

【2】.使用<%@ taglib %>引入标签库,设置uri和prefix的属性值(属性的值可以在jstl-impl-

1.2.2.jar/META-INF/c.tld找到)

【3】.使用标签。

注:具体使用请参考示例代码:
<%@page import="test_el_jstl.User"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%
	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 'jstl.jsp' starting page</title>
</head>
<body>
	<%
		request.setAttribute("weather", "今天天气很热!");

		pageContext.setAttribute("aaa", "aaa");
		request.setAttribute("aaa", "bbb");
		session.setAttribute("aaa", "ccc");
		application.setAttribute("aaa", "ddd");

		request.setAttribute("score", 78);
		
		
		List list = new ArrayList();
		User u1 = new User(1, "admin", "admin", "老王");
		User u2 = new User(2, "cai10", "admin", "蔡依林");
		User u3 = new User(3, "cai20", "admin", "蔡依林");
		User u4 = new User(4, "cai30", "admin", "蔡依林");
		User u5 = new User(5, "cai40", "admin", "蔡依林");
		list.add(u1);
		list.add(u2);
		list.add(u3);
		list.add(u4);
		list.add(u5);
		request.setAttribute("persons", list);
		
		
		Date date = new Date();
		request.setAttribute("d1", date);
		
		String dateStr = "1999/12/12 03:04:05";
		request.setAttribute("ds", dateStr);
		
		String str2 = "18810950652";
		request.setAttribute("phone", str2);
		
		
	%>
	<h3>c:out标签:用于将作用域中的对象输出,default属性会在value属性为空时显示</h3>
	<c:out value="${weather}" default="汗流浃背!"></c:out>

	<h3>c:set标签:用于将一个数据保存在作用域中(默认最小作用中),通过scope设定存放的作用域</h3>
	<c:set var="name" value="laowang" scope="request"></c:set>
	${requestScope.name}

	<c:set var="table">
		<table border="1" style="border-collapse: collapse;">
			<tr>
				<td>1*1</td>
				<td>1*2</td>
			</tr>
			<tr>
				<td>2*1</td>
				<td>2*2</td>
			</tr>
		</table>
	</c:set>
	${table}-${table}-${table}

	<h3>c:remove标签:将作用域中的指定key的数据删除(默认删除所有作用域中的指定key的数据)</h3>
	<c:remove var="aaa" scope="page" />
	${aaa}

	<h3>c:if标签:条件判断(单条件)</h3>
	<c:if test="${score >= 80}" var="flag">
		优秀!
	</c:if>
	<c:if test="${!flag}">
		良好!
	</c:if>
	
	<h3>c:choose 、 c:when、 c:otherwise标签:条件判断(多条件)</h3>
	<h4>注意以下几点:1.c:when必须在c:otherwise之前出现</h4>
	<h4>2.c:otherwise只能出现一次</h4>
	<h4>3.c:when和c:when和c:otherwise之前不允许出现其他任何字符</h4>
	<c:choose>
		<c:when test="${score >=80 }">
			优秀!
		</c:when>
		<c:when test="${score >=70 }">
			良好
		</c:when>
		<c:when test="${score >=60 }">
			及格
		</c:when>
		<c:otherwise>
			不及格		
		</c:otherwise>
	</c:choose>
	
	<h3>c:forEach标签:循环</h3>
	<h4>items:循环体,通过使用EL表达式获取循环的内容</h4>
	<h4>begin & end:只能为数字,当没有items属性时,从begin数字循环到end数字;当有items属性时,begin & end就表示下标</h4>
	<h4>step:步长</h4>
	<h4>var:表示每次的循环体</h4>
	<h4>varStatus:当前循环体的状态:有几个属性--first(判断当前循环体是否为第一个)、last(判断当前循环体是否为最后一个)、count(当前循环次数)、index(当前循环体的下标)</h4>
	<c:forEach begin="3" end="9" var="each">
		${each}
	</c:forEach>
	<br>
	<c:forEach items="4123,2512,222,111,555,677,999" begin="1" end="3" var="each">
		${each}
	</c:forEach>
	<br>
	<c:forEach items="${persons}" step="2" var="person">
		${person.username}<br>
	</c:forEach>
	<br>
	<c:forEach items="${persons}" var="person" step="2" varStatus="status">
		${person.username} --- ${status.first} --- ${status.last} --- ${status.count} --- ${status.index}<br>
	</c:forEach>
	
	<br>
	<table border="1" style="border-collapse: collapse;">
		<tr><td>id</td><td>用户名</td><td>真实姓名</td></tr>
		<c:forEach items="${persons}" var="person">
			<tr><td>${person.id}</td><td>${person.username }</td><td>${person.realname }</td></tr>
		</c:forEach>
	</table>
	
	<h3>c:redirect标签:重定向</h3>
	<%-- <c:redirect url="http://www.baidu.com"></c:redirect> --%>
	
	<h3>c:import标签:指定一个url的页面在本页面中</h3>
	<%--<c:import url="http://www.baidu.com"></c:import> --%>
	
	<fmt:formatDate value="${d1}" pattern="yyyy年MM月dd日 HH时mm分ss秒"/>
	<fmt:parseDate value="${ds}" var="d2" pattern="yyyy/MM/dd HH:mm:ss"></fmt:parseDate>
	
	${fn:length(phone) }
</body>
</html>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值