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