1EL表达式
2.1简介
EL表达式替代jsp表达式,因为开发jsp页面的时候遵守原则:在jsp页面中尽量少写甚至不写java代码。
EL表达式作用:
向浏览器输出域对象中的变量或表达式计算结果
基本语法${ 变量或表达式} 代替<%=变量或表达式%>
1)EL从四个域中自动选择
pageContext.setAttribute("name",name);
$[name]等价于 <%=pageContext.findAttribute("name")%>
2)EL从指定的域中获取
pageScope:page域
requestScope: request域
sessionScope: session域
applicationScope:application域
指定域获取的EL: $[requestScope.name];
2.2EL获取不同类型的数据
1)普通的字符串
2)普通的对象
EL:表达式的属性表示调用了对象的getXXX方法,例如
<%
Student student=new Student("eric","1234");
pageContext.setAttribute("student',student);
%>
student.name调用了student对象的getName()方法
${student}
对象的属性: ${student.name}
数组或者是集合List集合
<%
Student[] student=new Student[3];
student[0]=new student("1",‘123456“);
student[0]=new student("1",‘123456“);
student[0]=new student("1",‘123456“);
pageContext.setAttribute(stus",stus);
%>
数组:${student[0].name}
List集合
<%
List<Student> list=new ArrayList<Student>();
list.add(new student("1",‘123456“));
list.add(new student("1",‘123456“));
list.add(new student("1",‘123456“));
pageContext.setAttribute(list",list);
%>
${ list[0].name] $list{list.password}
Map集合
<%
Map<String,Student> map=new HashMap<String,Student>();
map.put("001",(new student("1",‘123456“));
map.put("001",(new student("1",‘123456“));
map.put("001",(new student("1",‘123456“));
pageContext.setAttribute(lmap",map);
%>
${map['001'].name}
2.3执行表达式
算术表达式
<%
int a=2;
int b=3;
pageContext.setAttribute(la"a);
pageContext.setAttribute("b",b);
%>
${a+b} ${a*b} ${a/b} ${a-b}
比较表达式
${a>b} ${a==b}
逻辑表达式
${true&&true}
判空表达式 empty
null:==null;
空字符串;=="";
null或空字符串
${name==null || name==""}
${empty name}既可以判断null也可以判断字符串空
2.4EL的11个内置对象
pageContext 等价于 jsp的pageContext内置对象
<%=pageContext.getRequest()%>
拿到上下文路径
${pageContext.request.contextPath}
pageScope 从指定的域中获取数据
requestScope
sessionScope
applocationScope
param 获取参数
<%=request.getParameter("name")%>
注意:param返回所有参数Map集合
${param['name ']}
paramValues
<%=request.getParameterValues("name")[0]%>
${paramValues['name'][0]}
header 获取请求头
<%=request.getHeader("host")%>
${header['host']}
headerValues
<%=request.getHeaders("host").nextElement()]%>
${headerValues['host'][0]}
cookie
<%=request.getCookies()[0].getValue()%>
${cookie['JSESSIONID'].name}
${cookie['JSESSIONID'].value}
initParam获取全局参数
<%=applocation.getInitParameter(’AAA')%>
${initParam['AAA']}