03EL表达式总结
EL表达式
综合理解
EL表达式用来获取后台传过来的作用域对象的数据的,比如后台setAttribute(“str”,“aaa”);那么这时候就可以用el将str获取到。
传统方式获取作用域对象的数据
EL获取作用域对象的数据
param和attribute
param一般是用于前台向后台请求数据的,也就是说是前台设置的参数。
attribute一般是用于后台设置参数返回到前台的,也就是说是后台设置的参数。
EL表达式中的${a+b} +只能用作加法运算,不能作为字符串的拼接。
cookie在request里面
四大作用域
pagecontext-》request-》session-》applicationContext
详细理解
https://www.cnblogs.com/czs1982/p/3966748.html
https://www.cnblogs.com/xdp-gacl/p/3938361.html
案例理解
<%@page import="java.util.*"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="com.amos.model.*"%>
<%@ page isELIgnored="false"%>
<%@ 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>
第一种,简单取值:
<%
String data = "hi,amos!";
request.setAttribute("data", data);
%>
${data}
<br> 第二种,从bean中取值:
<%
Person p = new Person();
p.setName("amosli");
session.setAttribute("person", p);
%>
${person.name}
<br>
<%=((Person) session.getAttribute("person")).getName()%><br>
第三种:从复杂bean中取值:
<%
Person person1 = new Person();
Address a = new Address();
a.setCity("上海");
person1.setAddress(a);
application.setAttribute("p1", person1);
%>
${p1.address.city}
<br> 第四种:从集合中取值:
<%
List list = new ArrayList();
list.add(new Person("amos"));
list.add(new Person("li"));
list.add(new Person("amosli"));
list.add(new Person("hi"));
list.add(new Person("hi_amos"));
application.setAttribute("personlist", list);
%>
${personlist[0].name }
<br>
<%
Map map = new HashMap();
map.put("aa", new Person("aaaa"));
map.put("cc", new Person("cccc"));
map.put("dd", new Person("dddd"));
map.put("ee", new Person("eeee"));
map.put("11", new Person("111"));
request.setAttribute("map", map);
%>
${map.dd.name }<br>
<%-- ${map.11.name } 一般情况下用点号进行取值,如果点号取不出来值,那么可以用[]来代替. --%>
${map['11'].name }<br>
获取当前应用的名称:
${pageContext.request.contextPath }
</body>
</html>