EL 技术
1.EL 表达式概述
EL(Express Lanuage)表达式可以嵌入在 jsp 页面内部,减少 jsp 脚本的编写,El 出现的目的是要代替jsp页面中脚本的编写。
2.EL 从域中取出数据(EL最重要的作用)
EL 最主要的作用是获得四大域中的数据,格式 $(EL 表达式)
EL 获得 pageContext 域中的值 :${pageContextScope.key}
EL 获得 request 域中的值 :${requestScope.key}
EL 获得 session 域中的值 :${sessionScope.key}
EL 获得 application 域中的值 :${applicationScope.key}
EL 获得域中某个值 :${key}
jsp 脚本 : request.getAttribute(“name”);
EL 表达式 : $(requestScope.name);
el.jsp
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import = "com.ma.domain.*" %>
<%@ page import = "java.util.*" %>
<!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.setAttribute("company", "越家影业");
//存储对象
User user = new User();
user.setId(1);
user.setName("ma");
user.setPassword("123456");
session.setAttribute("user",user);
//存储一个集合
List<User> list = new ArrayList<User>();
User user1 = new User();
user1.setId(1);
user1.setName("young");
user1.setPassword("123456");
list.add(user1);
User user2 = new User();
user1.setId(1);
user1.setName("wang");
user1.setPassword("123456");
list.add(user2);
User user3 = new User();
user1.setId(1);
user1.setName("tang");
user1.setPassword("123456");
list.add(user3);
application.setAttribute("list", list);
%>
<!--1、 脚本语句取值 -->
<%=request.getAttribute("company")
%>
<%
User sessionUser = (User)session.getAttribute("user");
out.write(sessionUser.getName());
%>
<!--2、 EL 表达式 -->
${requestScope.company}
${sessionScope.user.name}
${applicationScope.list[0].name}
<!--3、使用 el 表达式全域查找 -->
${company}
${user.name}
${list[1].name}
</body>
</html>
User.class
package com.ma.domain;
public class User {
private int id;
private String name;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
效果:
3.EL 的内置对象(11 个)
pageScope
requestScope
applicationScope
sessionScope(获取 jsp 中域的数据)
param.paramValues(相当于 request.getParameterValues();)
header.headerValues(相当于request.getHeader(name);)
initParam(获取全局初始化参数)
cookie(WEB 开发中 cookie)
pageContext(WEB 开发中的 pageContext)
4.EL 执行表达式
el.jsp
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import = "com.ma.domain.*" %>
<%@ page import = "java.util.*" %>
<!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.setAttribute("company", "越家影业");
//存储对象
User user = new User();
user.setId(1);
user.setName("ma");
user.setPassword("123456");
session.setAttribute("user",user);
//存储一个集合
List<User> list = new ArrayList<User>();
User user1 = new User();
user1.setId(1);
user1.setName("young");
user1.setPassword("123456");
list.add(user1);
User user2 = new User();
user1.setId(1);
user1.setName("wang");
user1.setPassword("123456");
list.add(user2);
User user3 = new User();
user1.setId(1);
user1.setName("tang");
user1.setPassword("123456");
list.add(user3);
application.setAttribute("list", list);
%>
<!--1、 脚本语句取值 -->
<%=request.getAttribute("company")
%>
<%
User sessionUser = (User)session.getAttribute("user");
out.write(sessionUser.getName());
%>
<!--2、 EL 表达式 -->
${sessionScope.user.name}
${applicationScope.list[0].name}
<!--3、使用 el 表达式全域查找 -->
${company}
${user.name}
${list[1].name}
<!-- EL 可以执行表达式运算 -->
${ 1+1 }
${1==1?true:false }
</body>
</html>
效果:
EL 也常用来标记文件目录:
form1.jsp
<%@ 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/form2.jsp" method="get">
姓名<input type="text" name="username"><br>
密码<input type="password" name="password"><br>
<input type="checkbox" name="hobby" value="zq">足球<br>
<input type="checkbox" name="hobby" value="pq">排球<br>
<input type="checkbox" name="hobby" value="ppq">乒乓球<br>
<input type="submit" value="提交"><br>
</form>
</body>
</html>
form2.jsp
<%@ 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>
${pageContext.request.contextPath}
</body>
</html>
效果:
提交表格后: