EL表达式和JSTL标签库

一.EL简介

使JSP代码写起来更简单(简单标签表现复杂逻辑和运算关系),可以给jsp标签的属性赋值,用来直接输出。

二.EL表达式

1.${EL表达式}访问属性

1.功能
①.访问Bean的属性:就是可以访问java类对象的属性
②.输出简单的运算结果
③.获取请求参数值:就是接受浏览器发送的一些请求的参数值


2.JavaBean定义
指公共类,含有参无参构造函数,属性和规范的set()get()方法


3.EL表达式访问Bean属性
①.${对象名.属性名}
②.${对象名["属性名"]}
例:${user.name}


4.user对象存储在作用域中
①.默认执行过程:${user.name}
从pageContext、request、session、application等作用域中依次查找绑定名为"user"的对象,直到从某个作用域中找到对象后,调用getName方法,将返回值输出(因为name属性是私有的,使用user.name属性就相当于调用getName()方法)。
②.手动设置从哪个作用域中查找user对象:找到了就返回回来,找不到就返回一个null,而且不会到比它高级的地方找

${pageScope.user.name}
${requestScope.user.name}
${sessionScope.user.name}
${applicationScope.user.name}

5.访问单独值
${变量名}——取代request.getProperty(变量名);


6.访问数组的属性
${user.数组名[index]}

创建bean包,创建User和Course两个实体类

//User实体类包括用户的姓名,年龄,课程,爱好,Course课程实体类包含课程id和课程名称,提供set、get方法,有参无参构造函数,toString方法
private String name;
private int age;
private Course course;
private String[] interest;
private int courseid;
private String coursename;

创建el1.jsp

<%@ page language="java" import="java.util.*,bean.*" pageEncoding="utf-8"%>
<html>
	<head>
		<meta http-equiv="content-Type" content="text/html; charset=utf-8">
	</head>
	<body>
		<%
			//创建Bean,在import中导bean.*包
			Course course=new Course(1,"java");
			User user=new User("kankang",18,course,new String[]{"shopping","cokking","swimming"});
			//将bean对象保存到request作用域中
			request.setAttribute("user",user);
			//也可以将基本数据类型保存到作用域中,在session中保存int userage
			session.setAttribute("userage",33);
		%>
		<!-- java代码 -->
		<%
			User getuser=(User)request.getAttribute("user");
			out.println(getuser.getName());
			out.println(getuser.getAge());
			out.println(getuser.getCourse().getCourseid());
			String[] interests=getuser.getInterest();
			for(String interest:interests){
				out.println(interest);
			}
		%>
		
		<br/>
		
		<!-- EL访问java类属性,数组 -->
		<!--使用EL表达式访问Bean属性,可以默认不写,按顺序查找--%>
		姓名:${requestScope.user.name}<br/>
		年龄:${user.age}<br/>
		课程号:${user.course.courseid}<br/>
		课程名:${user.course.coursename}<br/>
		第一个兴趣:${user.interest[0]}<br/>
		第二个兴趣:${user.interest[1]}
		
		<br/>
		
		<!-- EL访问基本数据类型 -->
		${sessionScope.userage}
		
	</body>
</html>

2.EL表达式进行计算

1.功能
①.用于给jsp标签属性赋值,也可以直接用来输出
在这里插入图片描述
②.算数运算:+,-,*,/,%(注意:+只能求和,不能连接字符串)
③.逻辑运算:&&,||,!
④.关系运算:>,>=,<,<=,==,!=,例: a g e > 10 ⑤ . e m p t y 运 算 : 空 运 算 主 要 用 于 判 断 字 符 串 , 集 合 是 否 为 空 , 例 : {age>10} ⑤.empty运算:空运算主要用于判断字符串,集合是否为空,例: age>10.empty{empty null}
四中空都输出为true:字符串为空,集合为空,值为null,找不到值

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
	<head>
		<meta http-equiv="content-Type" content="text/html; charset=utf-8">
	</head>
	<%
		request.setAttribute("name", "lemon");
		pageContext.setAttribute("str","123");
	%>
	<body>
		<%--使用EL表达式可以给input输入框赋值,使输入框里面默认值为lemon--%>
		<input type="text" value="${requestScope.name}"/>
		<span>${requestScope.name}</span>
		<%--EL表达式计算--%>
		<%--算数运算,只能数值类型做加法运算两个加双引号的不会做拼接,而是直接当做数值进行计算--%>
		${10+10}<br/>
		${"10"+"10"}<br/>
		<!-- 关系运算 -->
		${"123"=="123"}<br/>
		${"123" eq "123"}<br/>
		${"123" eq pageScope.str}<br/>
		<!-- empty运算,下面前四个个运算结果都为true,最后一个为false -->
		<%
			request.setAttribute("str1", "");
			List list=new ArrayList();
			request.setAttribute("list",list);
			request.setAttribute("obj",null);
		%>
		空字符串:${empty str1}<br/>
		空集合:${empty list}<br/>
		null:${empty  obj}<br/>
		不存在绑定名的对象:${empty xxx}
		直接来找一个字符串:${empty "aa"}
	</body>
</html>

3.EL获取请求参数

获得一个值:${param.username}与request.getParameter("username");
获得多个值:${paramValues.city[i]}与request.getParameterValues("city");

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
	<head>
		<meta http-equiv="content-Type" content="text/html; charset=utf-8">
	</head>
	<body>
		<!--通过EL获取请求参数-->
		<!--http://localhost:8080/el_jstl/el3.jsp?name=zs&interest=shopping&interest=swimming发起一个get请求-->
		<!--param.name会请求到在el3.jsp绑定的name=zs-->
		姓名:${param.name}<br/>
		兴趣1:${paramValues.interest[0]}<br/>
		兴趣2:${paramValues.interest[1]}<br/>
	</body>
</html>

三.JSTL简介

JSTL:Jsp Standard Tag Library(Jsp标准标签库)
是Apache组织基于Sun公司开发的JSP标签开发的一套标准的标签库,是JavaEE5.0核心
作用:使用简单的标签来表现复杂的逻辑运算关系
JSTL和EL结合替换页面中的Java代码:<% %>,<%= %>

四.JSTL标签库

1.如何使用JSTL

①.将标签库对应的jar包拷到WEB-INF/lib目录下
**jstl-1.2.jar**已经存在在Java EE 5 Libraries
②.使用taglib指令导入要使用的JSP标签:uri:标签的命名空间,prefix:命名空间的前缀

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

2.If标签

<c:if test="  "  var="  "  scope="  ">   </c:if>

test属性:相当于java中的if括号里的关系表达式(boolean表达式)
var属性:指定一个绑定名称存储test中boolean表达式的结果(true 或false)
scope属性:指定绑定范围(page、request、session、application)
注意:var 和scope要配合使用

3.choose标签

<c:choose>
	<c:when test="  "></c:when>
	<c:otherwrise></c:otherwise>
</c:choose>

choose标签相当于java中的switch,when表示一个处理分支,相当于switch的case标签,当test属性为true时会执行该分支,可以出现1次或多次,otherwise表示例外,可以出现0次或1次,相当于switch里面的default

4.forEach标签

<c:forEach var=" " item=" "></c:forEach>

作用:用来遍历集合或数组


属性
item:代表需要遍历的集合,会使用EL表达式从作用域中获取要遍历的集合对象
var:var中指定一个绑定名称,容器每次从集合中取出一个对象,然后绑定到pageContext对象上
varStatus:指定一个绑定名称,绑定值是由容器创建的对象,该对象封装了当前迭代的两种状态(Index和Count),Index:返回正在被迭代的对象的下标,从0开始;Count:放回第几次被迭代,从1开始,count永远比index大1

5.if、choose和forEach标签代码展示

<%@ page language="java" import="java.util.*,bean.*" pageEncoding="utf-8"%>
<!-- 引入标签库 -->
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
	<head>
		<meta http-equiv="content-Type" content="text/html; charset=utf-8">
		<style type="text/css">
			.s0{
				color:red;
			}
			.s1{
				color:yellow;
			}
		</style>
	</head>
	<body>
		<%
			//创建Employee对象, 并给属性赋值,且存入request作用域
			Employee employee=new Employee("tom","man1");
			request.setAttribute("employee",employee);
			//创建List<Employee>,给属性赋值,且存入session作用域
			List<Employee> employees=new ArrayList<Employee>();
			employees.add(new Employee("jim","man"));
			employees.add(new Employee("kangkang","man"));
			employees.add(new Employee("Jan","woman"));
			session.setAttribute("employees",employees);
		%>
		
		<h1>if标签</h1>
		姓名:${requestScope.employee.name}<br/>
		<!-- el获得绑定参数,var记录test结果的变量,scope确定var变量的作用范围,当rs为true时,输出男-->
		性别:<c:if test="${employee.gender=='man'}"  var="rs" scope="request"></c:if>
			<!-- 当rs为false时,输出女 ,rs保存在request作用域里面,所以要用EL获取-->
			<c:if test="${!rs}"></c:if>
		
		<br/>
		
		<h1>choose标签</h1>
		性别:<c:choose>
			<c:when test="${requestScope.employee.gender=='man'}"></c:when>
			<c:when test="${requestScope.employee.gender=='woman'}"></c:when>
			<c:otherwise>其他</c:otherwise>
		</c:choose>
		
		<br/>
		
		<h1>forEach标签</h1>
		<c:forEach var="employee" items="${sessionScope.employees}" varStatus="s">
			姓名:${employee.name}&nbsp;&nbsp;
			性别:
			<c:if test="${employee.gender=='man'}" var="rs"></c:if>
			<c:if test="${!rs}"></c:if>&nbsp;&nbsp;
			index:${s.index}&nbsp;&nbsp;
			count:${s.count}<br/>
		</c:forEach>
		
		<br/>
		
		<h1>forEach实现隔行变色的功能</h1>
		<c:forEach var="employee" items="${sessionScope.employees}" varStatus="s">
			<!-- 加上p标签实现隔行,给p标签加上class属性,如果是s0就将字体颜色变成红色,s1字体颜色变成黄色 -->
			<p class="s${s.index%2}">
			姓名:${employee.name}&nbsp;&nbsp;
			性别:
			<c:if test="${employee.gender=='man'}" var="rs"></c:if>
			<c:if test="${!rs}"></c:if>&nbsp;&nbsp;
			index:${s.index}&nbsp;&nbsp;
			count:${s.count}
			</p>
		</c:forEach>
	</body>
</html>

6.其他标签

<c:url  value='跳转页面地址'>     
			//实现跳转,相当于servlet里面的request.getRequestDispatcher(),
			//即使在禁用cookie的情况下也会像servlet里的url重写一样,
			//跳转到新的页面会在url地址中自动添加上JSESSIONID,保证能够访问session对象
<c:redirect url='重定向页面地址'>  
			//实现自动重定向跳转,相当于servlet里面的response.sendRedirect()
<c:set var="num" scope="session" value="${1+1}">     
			//作用域添加值,相当于servlet里面的setAttribute()
			//把变量为num,值为1+1的结果添加到session作用域中
<c:remove var="num" scope="session">
			//作用域移除值,相当于servlet里面的removeAttribute()
<c:catch var="msg"></c:catch>    
			//捕捉异常,将异常信息存入msg变量中
<c:import url="index.jsp"></c:import>
		   //将其他页面嵌入到当前页面中,相当于servlet里面的include(),c:import标签里面不能添加文本信息
<c:out value="${mothe}" default="hhh"/>
<c:out value="<table>" escapeXml="false"/>
      	   //输出变量,default为找不到时默认输出,
      	   //out的escapeXml属性可以过滤特殊字符,默认为true,设置为false才能过滤特殊字符

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!-- 引入标签库 -->
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
	<head>
		<meta http-equiv="content-Type" content="text/html; charset=utf-8">
	</head>
	<body>
		<h1>c:url实现跳转</h1>
		<!-- 点击按钮,或加一个a标签实现跳转 -->
		<!--绑定一个name参数到session作用域中-->
		<%session.setAttribute("name","hello Kitty");%>
		<a href="<c:url value='jstl3.jsp'/>">跳转到jstl3.jsp</a>
	
		<br/>
		
		<h1>c:redirect重定向</h1>	
		<!-- 重定向不需要点击跳转 -->
		<!--绑定一个age参数到session作用域中-->
		<%session.setAttribute("age",100);%>
		<%--<c:redirect url="jstl3.jsp"/>--%>		
		<br/>
		
		<h1>c:set在作用域添加值,c:remove在作用域移除值</h1>
		<!-- 把变量为num,值为1+1的结果添加到session作用域中 -->
		<c:set var="num" scope="session" value="${1+1}"></c:set>
		set之后:num=${sessionScope.num}
		<c:remove var="num" scope="session"/>
		remove之后:num=${sessionScope.num}
		
		<h1>c:catch标签获得异常</h1>
		<!--异常信息保存在var变量中-->
		<c:catch var="msg">
			<%Integer.parseInt("123a");%>
		</c:catch>
		异常信息:${msg}
		
		<br/>
		
		<h1>c:import将其他页面嵌入到当前页面中</h1>
		<!--c:import标签里面不能添加文本信息-->
		<c:import url="index.jsp"></c:import>
		
		<br/>
		<h1>c:out输出变量</h1>
		<!-- default为找不到时默认输出 -->
		<%session.setAttribute("mother","xiaoyou");%>
		<c:out value="${mothe}" default="hhh"/><br/>
		<!-- out的escapeXml属性可以过滤特殊字符,默认为true,设置为false才能过滤特殊字符 -->
		<c:out value="<table>"/>
		<c:out value="<table>" escapeXml="false"/>
	</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值