EL运算符,表达式 及 域对象中key相同问题
程序员文章站
2022-05-28 22:28:53
...
EL表达式
EL简介
概述:建立在jsp2.0的一门查询语言、它的灵感是来自于xpath、ECMAScript。主要作用专门用于获取servlet中保存在域对象中的内容。
语法: ${ 域对象中的key }
EL中的运算符
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
request.setAttribute("msg", 10);
ArrayList<String> list= new ArrayList<String>();
list.add("孙悟空");
list.add("猪八戒");
list.add("沙悟净");
list.add("老唐僧");
request.setAttribute("list", list);
%>
<!-- 算术运算符 -->
${msg+10 }<br>
${msg-10 }<br>
${msg*10 }<br>
${msg/10 }<br>
${msg div 10 }<br>
${msg%10 }<br>
${msg mod 10 }<br>
<br>
<!-- 逻辑运算符 -->
${msg>10 && msg<=20 }<br>
${msg>10 and msg<=20 }<br>
${msg>10 || msg<=20 }<br>
${msg>10 or msg<=20 }<br>
${!(msg>10) }<br>
${not(msg>10) }<br>
<br>
<!-- 关系运算符 -->
${msg>10 }<br>
${msg gt 10}<br>
${msg<10 }<br>
${msg lt 10}<br>
${msg==10 }<br>
${msg eq 10}<br>
${msg!=10 }<br>
${msg ne 10}<br>
${msg>=10 }<br>
${msg ge 10}<br>
${msg<=10 }<br>
${msg le 10}<br>
<br>
<!-- 判空 -->
${list!=null }<br>
${not empty list}<br>
${list==null }<br>
${empty list }<br>
<br>
<!-- 三元表达式 -->
${msg>9?"大于":"小于" }
</body>
</html>
EL表达式的使用
<%@page import="java.util.HashMap"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.offcn.entity.Student"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
request.setAttribute("msg", "天街小雨润如酥,草色遥看近却无");
Student stu= new Student(1,"pgone","男","少妇","高中","尤其喜欢明星的老婆");
Student stu1= new Student(2,"宋哲","男","老板老婆","高中","尤其喜欢王宝强的老婆");
Student stu2= new Student(3,"罗志祥","男","多人运动","高中","一月三次一次三天");
request.setAttribute("stu", stu);
String arr[] ={"马保国","药水哥","切克哇啦"};
request.setAttribute("arr1", arr);
ArrayList<String> list =new ArrayList<String>();
list.add("仓啊空");
list.add("武藤兰");
list.add("玛瑞亚");
request.setAttribute("list", list);
ArrayList<Student> stuList= new ArrayList<Student>();
stuList.add(stu);
stuList.add(stu1);
stuList.add(stu2);
request.setAttribute("stuList", stuList);
HashMap<String,String> map= new HashMap<String,String>();
map.put("it001", "孙悟空");
map.put("it002", "二郎神");
map.put("it003", "哮天犬");
request.setAttribute("map", map);
HashMap<String,Student> map1= new HashMap<String,Student>();
map1.put("it1", stu);
map1.put("it2", stu1);
map1.put("it3", stu2);
request.setAttribute("map1", map1);
%>
<!-- 1. 取变量的值 -->
${msg } <br>
<!-- 2.取对象属性的值 底层走的get方法 -->
${stu.name } ${stu.hobby }<br>
<!-- 3.取数组的值:使用下标 -->
${arr1[0] } ${arr1[1] } ${arr1[2] } <br>
<!-- 4.取集合的值 -->
${list[0] } ${list[1] } ${list[2] }<br>
${list.get(0) } ${list.get(1) } ${list.get(2) }<br>
<!-- 4.取集合对象的值 -->
${stuList.get(0).name } ${stuList.get(0).sex } ${stuList.get(0).hobby } ${stuList.get(0).sdesc }<br>
${stuList.get(1).name } ${stuList.get(1).sex } ${stuList.get(1).hobby } ${stuList.get(1).sdesc }<br>
${stuList.get(2).name } ${stuList.get(2).sex } ${stuList.get(2).hobby } ${stuList.get(2).sdesc }<br>
<!-- 5.Map集合: -->
${map.it001 } ${map.it002 } ${map.it003 } <br>
<!-- 5.Map集合对象的值: -->
${map1.it1.name } ${map1.it1.hobby } ${map1.it1.sdesc } <br>
${map1.it2.name } ${map1.it2.hobby } ${map1.it2.sdesc } <br>
${map1.it3.name } ${map1.it3.hobby } ${map1.it3.sdesc } <br>
</body>
</html>
EL进阶
如果域对象中出现了相同的key、我们使用el表达式获取域对象中的内容、默认情况下从最小的域对象中开始获取。如果我们想从指定域获取可以通过xx.scope的方式获取