1.EL的概述
EL为了使JSP写起来更加简单
语法:${EL表达式}
功能:获取数据,执行运算
2.EL获取域对象中的数据
<% //pageContext.setAttribute("name", "pValue"); //request.setAttribute("name", "rValue"); //session.setAttribute("name", "sValue"); application.setAttribute("name", "aValue"); %> <%=pageContext.getAttribute("name") %> <!-- 如果没找到 返回null --> <%=request.getAttribute("name") %> <%=session.getAttribute("name") %> <%=application.getAttribute("name") %> <hr/> ${ pageScope.name } <!-- 返回的是"" --> ${ requestScope.name } ${ sessionScope.name } ${ applicationScope.name } <hr/>
${name}就类似于findAttribute("name"),它会先从page域中查找,没找到去request域中查询,没有找到去session域中找,最后去application中找
3.EL获取数组中的数据
<% String[] arrs = {"张三","李四希","王五","赵六"}; pageContext.setAttribute("arrs", arrs); %> ${ arrs[0] } ${ arrs[1] } ${ arrs[2] } ${ arrs[3] }
4.获取集合中的数据
list集合:
<% List<String> list = new ArrayList<String>(); list.add("张三"); list.add("李四"); list.add("王五凤"); pageContext.setAttribute("list", list); %> ${ list[0] } ${ list[1] } ${ list[2] }
map集合
<% Map<String,String> map = new HashMap<String,String>(); map.put("aaa","张三"); map.put("bbb","李四"); map.put("ccc","王五"); pageContext.setAttribute("map", map); %> ${ map.aaa } ${ map.bbb } ${ map.ccc }
5.获取对象的数据
<% User user = new User(1,"aaa","123"); pageContext.setAttribute("user", user); %> ${ user.id } ${ user.username } ${ user.password }
6.获取对象的集合的数据
<% User user1 = new User(1,"aaa","123"); User user2 = new User(2,"bbb","123"); User user3 = new User(3,"ccc","123"); List<User> userList = new ArrayList<User>(); userList.add(user1); userList.add(user2); userList.add(user3); pageContext.setAttribute("userList", userList); %>
${ userList[0].id } - ${ userList[0].username } - ${ userList[0].password }<br/>
${ userList[1].id } - ${ userList[1].username } - ${ userList[1].password }<br/>
${ userList[2].id } - ${ userList[2].username } - ${ userList[2].password }<br/>
注意:
.和[]的区别:[]用于有下标的数据(数组,list集合),用于有属性的数据(map,对象),如果属性名中包含有特殊的字符,必须使用[]
7.EL执行运算
执行算数运算
<% pageContext.setAttribute("n1", "10"); pageContext.setAttribute("n2", "20"); pageContext.setAttribute("n3", "30"); pageContext.setAttribute("n4", "40"); %> ${ n1 + n2 + n3 }
执行逻辑运算
${ n1 < n2 } - ${ n1 lt n2 } <br/> ${ n1 > n2 } - ${ n1 gt n2 } <br/> ${ n1 <= n2 } - ${ n1 le n2 }<br/> ${ n1 >= n2 } - ${ n1 ge n2 } <br/> ${ n1 == n2 } - ${ n1 eq n2 } <br/>
EL执行关系运算
${ n1<n2 && n3 < n4 } - ${ n1<n2 and n3 < n4 }<br/> ${ n1<n2 || n3 < n4 } - ${ n1<n2 or n3 < n4 }<br/> ${ !(n1 < n2) } - ${ not(n1<n2) }
执行三元运算
${ n1 < n2 ? "正确":"错误" }
emp运算
${ user == null } - ${ empty user } ${ user != null } - ${ not empty user }
8.EL常用的11个对象
<!-- pageScope,requestScope,sessionScope,applicationScope - 获取JSP中域中的数据 param,paramValues - 接收参数. header,headerValues - 获取请求头信息 initParam - 获取全局初始化参数 cookie - WEB开发中cookie pageContext - WEB开发中的pageContext. -->
9.JSTL的标签库
core(核心标签),fmt(国际化标签),xml(XML标签),sql(SQL标签),fn(JSTL提供EL函数库)
引入JSTL的相关的jar包.
在页面中引入标签库.<%@ taglib uri=”” prefix=””%>
10.EL提供的函数
${ fn:contains("Hello World","Hello") } ${ fn:length("HelloWorld") } ${ fn:toLowerCase("ABCDE") } <c:forEach var="i" items='${ fn:split("a-b-c-d","-") }'> ${ i } </c:forEach>
11.页面显示商品的部分代码
<c:forEach var="p" items="${list }"> <tr> <td>${ p.pid }</td> <td>${ p.pname }</td> <td>${ p.price }</td> <td> <c:if test="${ p.hot == 1 }"> 是 </c:if> <c:if test="${ p.hot != 1 }"> 否 </c:if> </td> <td>${ p.pdesc }</td> </tr> </c:forEach>