<%@ page import="test_el_jstl.Address"%>
<%@ page import="test_el_jstl.User"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page isELIgnored="false" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
<%--
加判断,避免空指针异常
<%
User user = (User)session.getAttribute("user");
if(user != null){
out.write("ki16,欢迎"+user.getRealname()+"光临本站!");
}else{
out.write("ki16,欢迎光临本站!");
}
%> --%>
<%-- 可能会报500异常:
ki16,欢迎<%= ((User)session.getAttribute("user")).getRealname() %>光临本站! --%>
<%-- 更为简单,稳妥的方式! --%>
<%
int a = 10;
//数据在四大作用域对象中
pageContext.setAttribute("a", a);
request.setAttribute("aaa", true);
session.setAttribute("age", 18);
application.setAttribute("name", "laowang");
// 简单对象
User user = new User(1, "admin", "admin", "老王");
request.setAttribute("uuu", user);
// 复合对象
User u2 = new User(2, "admin-laowang", "123123", "老王", new Address(
"广东省", "广州市", "天河区"));
pageContext.setAttribute("u2", u2);
//List中的数据
List list = new ArrayList();
list.add("apple");
list.add("orange");
list.add("watermelon");
list.add("pineapple");
pageContext.setAttribute("fruits", list);
//Map中的数据
Map map = new HashMap();
map.put("name", "zhangsan");
map.put("age", 18);
map.put("gender", "女");
map.put("address", new Address("广东省", "广州市", "天河区"));
pageContext.setAttribute("person1", map);
//作用域中有相同的key
pageContext.setAttribute("xxx", "pageContext");
request.setAttribute("xxx", "request");
session.setAttribute("xxx", "session");
application.setAttribute("xxx", "application");
User uu1 = new User(5,"admin","admin","张三");
User uu2 = new User(5,"admin","admin","张三");
request.setAttribute("uu1", uu1);
request.setAttribute("uu2", uu2);
request.setAttribute("score", 99);
pageContext.setAttribute("a1", null);
pageContext.setAttribute("a2", new ArrayList());
pageContext.setAttribute("a3", new HashMap());
pageContext.setAttribute("a4", "");
%>
欢迎${user.realname}光临本站!
<h3>EL表达式从哪里取值?一般情况下,从作用域对象中取值</h3>
${a} --- ${aaa} --- ${age} --- ${name}
<h3>EL表达式可以取什么类型的值?基本数据类型、引用数据类型(对象、复合对象、List、Map)</h3>
${uuu.realname} --- ${u2.addr.town} --- ${fruits[2]} ---
${person1.address.city}
<h3>EL表达式从哪个作用域中取值?默认从最小作用域中开始寻找,找不到往大的作用域中找,都找不到显示为空</h3>
${xxx}
<h3>EL从指定作用域中取值?
pageScope、requestScope、sessionScope、applicationScope</h3>
${pageScope.xxx }--- ${requestScope.xxx} --- ${sessionScope.xxx } ---
${applicationScope.xxx }
<h3>EL表达式还可以取请求参数中的值:param、paramValues</h3>
${param.username} --- ${paramValues.fav[2] }
<h3>EL表达式除了“.”操作符之外,还有“[ ]”操作符:“[]”可以解析子EL表达式</h3>
${uuu["realname"]} --- ${param.type} --- ${uuu[param.type]}
<h3>EL表达式中的为空判断:empty判断为空的有:空字符串、null、空集合、空map</h3>
${empty a1} --- ${empty a2} --- ${empty a3} --- ${empty a4}
<h3>EL表达式可以进行一些基本的数学运算</h3>
${5+4} --- ${5-4} --- ${5*4} --- ${5/4} --- ${5%4} --- ${5+"4"}
<h3>EL表达式可以进行一些逻辑判断</h3>
${5>4} --- ${ 5 == 4} --- ${ 5 eq 4 } --- ${uu1 == uu2} --- ${uu1 eq uu2} --- ${score == 99 }
</body>
</html>