EL技术
- EL 表达式概述
EL(Express Lanuage)表达式可以嵌入在jsp页面内部,减少jsp脚本的编写,EL 出现的目的是要替代jsp页面中脚本的编写。
2.EL从域中取出数据(EL最重要的作用)
jsp脚本:<%=request.getAttribute(name)%>
EL表达式替代上面的脚本:${requestScope.name}
EL最主要的作用是获得四大域中的数据,格式${EL表达式}
EL获得pageContext域中的值:${pageScope.key};
EL获得request域中的值:${requestScope.key};
EL获得session域中的值:${sessionScope.key};
EL获得application域中的值:${applicationScope.key};
EL从四个域中获得某个值${key};
---同样是依次从pageContext域,request域,session域,application域中 获取属性,在某个域中获取后将不在向后寻找
- 获得普通字符串
- 获得User对象的值
- 获得List<User>的值
3. EL执行表达式
例如:
${1+1}
${empty user}
${user==null?true:false}
<%@page import="java.util.*"%>
<%@page import="com.xiaowei.domain.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- 模拟存储数据 -->
<%
//向request域中存储一个姓名
request.setAttribute("name", "liumengfei");
pageContext.setAttribute("name", "xiaowei");
//向session域中存储一个user
User user = new User();
user.setId("1");
user.setName("zhangsan");
user.setPassword("123");
session.setAttribute("user", user);
//向application域中存储一个集合
//创建一个集合
List<User> list = new ArrayList<User>();
User user1 = new User();
user1.setId("2");
user1.setName("wuyong");
user1.setPassword("123");
list.add(user1);
User user2 = new User();
user2.setId("3");
user2.setName("ziyou");
user2.setPassword("123");
list.add(user2);
application.setAttribute("list", list);
%>
<!-- 使用脚本获取域中的值 -->
<%=request.getAttribute("name") %>
<%
User sessionUser = (User)session.getAttribute("user");
out.write(sessionUser.getName());
%>
<!-- 这种方法存储集合太麻烦 就不用这 -->
<!-- 使用el获取域中的值 -->
${requestScope.name }
${sessionScope.user.name }
${applicationScope.list[1].name }
<br>
<!-- 使用el表达式全局查找 -->
${name }
${user.name }
${list[1].name }
<br>
<!-- el可以执行表达式运算 -->
${1+4 }
${3==3?true:false }
<!-- 判断list是否为空 如果为空返回true 如果不为空返回false -->
${empty list }
</body>
</html>
4.EL的内置对象11个(了解)
pageScope,requestScope,sessionScope,applicationScope
---- 获取JSP中域中的数据
param,paramValues - 接收参数.
相当于request.getParameter() rquest.getParameterValues()
header,headerValues - 获取请求头信息
相当于request.getHeader(name)
initParam - 获取全局初始化参数
相当于this.getServletContext().getInitParameter(name)
cookie - WEB开发中cookie
相当于request.getCookies()---cookie.getName()---cookie.getValue()
pageContext - WEB开发中的pageContext. (掌握)
pageContext获得其他八大对象
${pageContext.request.contextPath}
相当于<%=pageContext.getRequest().getContextPath%> 这句代码不能实现
获得WEB应用的名称
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/el/form1.jsp"
method="post">
<input type="text" name="username"><br> <input
type="password" name="password"><br> <input
type="checkbox" name="hobby" value="zq">足球 <input
type="checkbox" name="hobby" value="pq">排球 <input
type="checkbox" name="hobby" value="ppq">乒乓球<br> <input
type="submit" value="提交"><br>
</form>
<img alt="" src="${pageContext.request.contextPath }/1.jpg">
<img alt="" src="${pageContext.request.contextPath }/2.jpg">
<img alt="" src="1.jpg">
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
kkkkkkkkkkkkkkk
</body>
</html>