EL表达式

什么是El表达式

从jsp2.0开始,他是jsp的内置表达式语言,不需要导包.

 

EL主要作用

1)获取数据:

EL表达式主要用于替换JSP页面中的脚本表达式,以从各种类型的web域中检索java对象、获取数据。(某个web域 中的对象,访问javabean的属性、访问list集合、访问map集合、访问数组)

2)执行运算:

利用EL表达式可以在JSP页面中执行一些基本的关系运算、逻辑运算和算术运算,以在JSP页面中完成一些简单的逻辑运算。${user==null}

 

3)获取web开发常用对象

EL 表达式定义了一些隐式对象,利用这些隐式对象,web开发人员可以很轻松获得对web常用对象的引用,从而获得这些对象中的数据。

 

4)调用Java方法

EL表达式允许用户开发自定义EL函数,以在JSP页面中通过EL表达式调用Java类的方法。

 

EL的语法

格式: ${ 变量名或者表达式 }

注意:el表达式只能够获取域中的数据,在el中,所有的get方法,在调用的时候都可以省略get前缀不写。

 

可以得到的数据类型

1)普通字符串
2)自定义普通对象
3)List集合
4)Map集合

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8" import="blog.csdn.net.model.*,java.util.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>el表达式</title>
</head>
<body>
	<%
		//把数据放入pageContext域中
		pageContext.setAttribute("student", new Student("chenys", 20));
		
		//List集合
		List<Student> list = new ArrayList<Student>();
		list.add(new Student("rose", 18));
		list.add(new Student("lucy", 20));
		list.add(new Student("jack", 25));
		pageContext.setAttribute("list", list);
 
		//Map集合
		Map<String, Student> map = new HashMap<String, Student>();
		map.put("100", new Student("lily", 18));
		map.put("101", new Student("mark", 18));
		map.put("102", new Student("狗娃", 8));
		pageContext.setAttribute("map", map);
                
        //数组
        pageContext.setAttribute("arr",new String[]{"aa","bb","cc"});
 
     %>


	<!-- 1.获取普通对象的数据,获取顺序是pageContext > request > session > application -->
	姓名:${student.name}<br /> 
	年龄:${student.age }<br />
	<!-- 等价于 -->
	<%=((Student) pageContext.getAttribute("student")).getName()%><br />
	<%=((Student) pageContext.getAttribute("student")).getAge()%><br />
	<hr />

	<!-- 2.获取List集合的数据 -->
	姓名:${list[0].name } - 年龄:${list[0].age }<br />
	姓名:${list[1].name } - 年龄:${list[1].age }<br />
	姓名:${list[2].name } - 年龄:${list[2].age }<br />
	<%--等价于 --%>
	<%
   		List<Student> stus =(List<Student>) pageContext.getAttribute("list");
   		for(Student stu : stus){
   			out.write("姓名:"+stu.getName()+" - "+ "年龄:"+stu.getAge()+"<br/>");
   		}
   	%>
	<hr />

	<!-- 3.获取map集合的数据 -->
	姓名:${map['100'].name } - 年龄:${map['100'].age }<br /> 
	姓名:${map['101'].name } - 年龄:${map['101'].age }<br /> 
	姓名:${map['102'].name } - 年龄:${map['102'].age }<br />
	<%--等价于 --%>
	<%
	   Map<Integer,Student>mps = (Map<Integer,Student>)pageContext.getAttribute("map");
	   for(Map.Entry<Integer,Student> entrys : mps.entrySet()){
	   	out.write("姓名:"+entrys.getValue().getName()+" - "+ "年龄:"+entrys.getValue().getAge()+"<br/>");
	   }
	%>
	
	<!-- 4.获取数组数据 -->
	${arr[0]}<br /> 
	${arr[1]}<br /> 
	${arr[2]}<br /> 
	<%--等价于 --%>
	<%=((String[])pageContext.getAttribute("arr"))[0]%>
	<%=((String[])pageContext.getAttribute("arr"))[1]%>
	<%=((String[])pageContext.getAttribute("arr"))[2]%>
</body>
</html>

EL进行运算

1)算术运算

2)比较运算

3)逻辑运算

4)判空运算

 

demo示例:

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>el运算</title>
</head>
<body>
	<%
	pageContext.setAttribute("a", 10);
	pageContext.setAttribute("b", 4);
    	pageContext.setAttribute("name1","");
    	pageContext.setAttribute("name2",null);
	%>
	<!-- 1.算术运算 -->
	${a+b }<br /> 
	${a-b }<br /> 
	${a*b }<br /> 
	${a/b }<br />
	${a%b }<br />
	
	<!-- 2.比较运算 -->
	${a>b }<br/>
	${7<5 }<br/>
	${a==b }<br />
	
	<!-- 3.逻辑运算 -->
	${true&&true }<br/>
 	${(a>b)|| false }<br/>
 	${!(a>b) }<br/>
        ${a>b?"yes":"no"}
	
	<!-- 4.判空运算 -->
	${name1==null}<br/>
   	<%-- empty: 既能判null,也能判断空字符串 --%>
    	${empty name2 } 表示判断name2的值是否为空
        ${!empty name2 }或者 ${not empty name2} 表示判断name2的值是否不为空
    
</body>
</html>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值