Web
来源:互联网 发布:sql server 触发器 编辑:程序博客网 时间:2024/06/16 20:12
【EL的概述】
EL:Expression Language:表达式语言
简化JSP的代码,而且减少<%%>
语法:${EL 表达式}
EL的功能:
*获取数据的:(JSP的四个域)
*执行运算
*操作web开发的常用的对象
*调用Java中的方法(不常用)
【获取数据】
<h1>EL功能:获取数据</h1> <% pageContext.setAttribute("pname", "pvalue"); request.setAttribute("rname", "rvalue"); session.setAttribute("sname", "svalue"); application.setAttribute("aname", "avalue"); %><%= pageContext.getAttribute("pageNmae") %><!-- 如果找不到就返回NUll --><hr>${ pageScope.pname } <!-- 如果找不到,就返回“”,空值 -->${ requestScope.rname}${ sessionScope.pname }${ applicationScope.rname}
获取数组
<% String[] arrs = {"tommy","allen","hans"}; pageContext.setAttribute("arrs", arrs); %> ${ pageScope.arrs[0] }
获取List集合
<!-- 招数据,先从page-request-session-application --> <hr> <% String[] arrs = {"tommy","allen","hans"}; pageContext.setAttribute("arrs", arrs); %> ${ pageScope.arrs[0] } <% List<String> list = new ArrayList<String>(); list.add("tommy"); list.add("allen"); pageContext.setAttribute("list", list); %> ${ list[0] } ${ list[1] } <hr/> <% Map map = new HashMap(); map.put("aaa", "tommy"); map.put("bbb", "allen"); pageContext.setAttribute("map", map); %> ${ map.aaa } ${ map.bbb }
* . 和[] 的区别
[] 用于有下标的数据(数组,List集合),用于有属性的数据(map,对象)
如果属性名中包含特殊的字符,必须使用[]
<hr/> <% Map map = new HashMap(); map.put("aaa", "tommy"); map.put("bbb", "allen"); pageContext.setAttribute("map", map); %> ${ map.aaa } ${ map.bbb } ${ map["ddd.eee"] }
<hr/> <!-- 获得对象的属性值 --> <% User user = new User(1,"aaa","123"); pageContext.setAttribute("user", user); %> ${ user.id } ${ user.username } ${ user.password } <h3>获取对象集合的数据</h3> <% User user1 = new User(1,"aaa","123"); User user2 = new User(1,"bbb","123"); User user3 = new User(1,"ccc","123"); List userlist = new ArrayList(); userlist.add(user1); userlist.add(user2); userlist.add(user3); pageContext.setAttribute("userlist", user); %> ${ 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 }
【EL执行运算】
【执行数学运算】
<% pageContext.setAttribute("n1", 10); pageContext.setAttribute("n2", 20);%>${ n1+n2 }<h1>el执行逻辑运算</h1>${ n1 < n2 } - ${ n1 lt n2 } <!-- lt = less than -->${ n1 <= n2 } - ${ n1 le n2 } <!-- le = less equal -->${ n1 > n2 } - ${ n1 gt n2 } <!-- great than -->${ n1 >= n2 } - ${ n1 ge n2 } <!-- great equal -->${ n1 == n2 } - ${ n1 eq n2 }<h1>el不能执行关系运算</h1>${ n1 < n2 && n3 < n4 } - ${ n1 lt n2 and n3 lt n4 }${ n1 < n2 || n3 < n4 } - ${ n1 lt n2 or n3 lt n4 }${ !(n1 < n2) } - ${ not(n1 lt n2) }<h1>el执行三元运算</h1>${ n1 < n2 ? "true" : "false" }<h1>empty 运算</h1>${ user == null } - ${ empty user }${ user != null } - ${ not empty user }
【El操作Web开发的常用对象】
<h1>操作web开发的常用的对象</h1><!-- pageScope,requestScope,sessionScope,applicationScope,param,paramValues -- 接受参数 --><!-- header,headerValues -- 获取请求体 --><!-- initParmm -- 获取全局初始化参数 --><!-- cookie -- WEB开发中cookies --><!-- pageContext -- Web 开发中的pageContext --><% //获得其他8个内置对象 --- 主要在编写框架,通用性很高的代码中 request.setAttribute("id", "001"); request.setAttribute("name", "name");%><%= request.getAttribute("id") %><%= request.getAttribute("name") %><!-- 接受请求的参数 --><%= request.getParameter("ids") %><%= request.getParameter("names") %><%= request.getParameter("hobbies") %><hr>${ requestScope.id }${ requestScope.name }<!-- http://localhost:8080/day12JSP/demo05-EL/ELDemo3.jsp?ids=001&names=zuochunhui -->${ requestScope.ids }${ requestScope.names }${ paramValues.hobbies[0] }${ paramValues.hobbies[1] }<!-- 001 zuochunhui 001 name basket football --><hr><!-- 获取请求头 --><%= request.getHeader("User-Agent") %><hr>${ header["User-Agent"] }<!-- Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko --><h3>获取全局初始化参数</h3><!-- <context-param> <param-name>username</param-name> <param-value>root</param-value> </context-param> -->${ initParam.username }${ initParam.value }<h3>获取cookies中的值</h3>${ cookie.history.value }<h3>pageContext中的对象</h3><!-- get Ip address -->${ pageContext.request.remoteAddr }<!-- 获取工程路径 -->${ pageContext.request.contextPath }