1.el的作用
使用EL表达式替换掉jsp表达式
**EL表达式作用: 向浏览器输出(域对象中)的变量值或表达式计算的结果!!!**
语法: ${变量或表达式}
2.EL的语法
1)输出基本数据类型变量
1.1 从四个域获取
${name}
1.2 指定域获取
${pageScope.name}
域范围: pageScoep / requestScope / sessionScope / applicationScope
<body>
<%
String name = "rose";
//放入域中
//pageContext.setAttribute("name",name);
pageContext.setAttribute("name",name,PageContext.REQUEST_SCOPE);
%>
<%=name %>
<br/>
<%--
1)从四个域自动搜索
--%>
EL表达式: ${name }
<%--
${name } 等价于
<%=pageContext.findAttribute("name")%>
--%>
<%--
2) 从指定的域中获取数据
--%>
EL表达式: ${pageScope.name }
<%--
${pageScope.name } 等价于
<%= pageContext.getAttribute("name",PageContext.PAGE_SCOPE)%>
--%>
</body>
2)输出对象的属性值
<body>
<%--
1)EL输出对象的属性
--%>
<%
//保存数据
Student student = new Student("eric",20);
//放入域中
pageContext.setAttribute("student",student);
//List
List<Student> list = new ArrayList<Student>();
list.add(new Student("rose",18));
list.add(new Student("jack",28));
list.add(new Student("lucy",38));
//放入域中
pageContext.setAttribute("list",list);
//Map
Map<String,Student> map = new HashMap<String,Student>();
map.put("100",new Student("mark",20));
map.put("101",new Student("maxwell",30));
map.put("102",new Student("narci",40));
//放入域中
pageContext.setAttribute("map",map);
%>
<%--使用EL获取对象 --%>
${student.name} - ${student.age }
<%--
${student.name} 等价于 (点相对于调用getXX()方法 ,get方法出去get,后全部小写)
<%=((Student)pageContext.findAttribute("student")).getName()%>
--%>
<hr/>
<%--使用EL获取List对象 --%>
${list[0].name } - ${list[0].age }<br/>
${list[1].name } - ${list[1].age }<br/>
${list[2].name } - ${list[2].age }
<%--
list[0]等价于 (中括号相对于调用get(参数)方法)
((List)pageContext.findAttribute("list")).get(0)
--%>
<hr/>
<%--使用EL获取Map对象 --%>
${map['100'].name } - ${map['100'].age }<br/>
${map['101'].name } - ${map['101'].age }<br/>
${map['102'].name } - ${map['102'].age }<br/>
</body>
3)输出集合对象
List 和 Map
4)EL表达式计算
<body>
<%--
1)算术表达式
+ - * /
--%>
${10+5 }<br/>
${10*5 }
<hr/>
<%--
2)比较运算
> < >= <= == !=
--%>
${10>5 }<br/>
${10<5 }<br/>
${10!=10 }
<hr/>
<%--
3)逻辑运算
&& || !
--%>
${true && false }<br/>
${true || false }<br/>
${!false }<br/>
<hr/>
<%--
4)判空
null 或 空字符串: empty
--%>
<%
//String name = "eric";
//String name = null;
String name = "";
pageContext.setAttribute("name",name);
%>
判断null: ${name==null }<br/>
判断空字符: ${name=="" }<br/>
判空: ${name==null || name=="" }
另一种判空写法: ${empty name }
</body>