目录
1.EL表达式介绍
EL(Expression Language) 是为了使JSP写起来更加简单。
2.EL表达式内置对象
3.EL表达式访问4种范围属性
寻找值的顺序:page->request->session->application
<%@ 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.setAttribute("info1", "page范围");
request.setAttribute("info1", "request范围");
session.setAttribute("info1", "session范围");
application.setAttribute("info1", "application范围");
%>
<!-- 最后取的是page范围的值 -->
<h1>${info1 }</h1>
</body>
</html>
运行结果:
4.EL表达式接受请求参数
Param:单个参数 paramValues:一组参数
<%@ 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>
<!-- param一个参数传值 -->
<form action="el2.jsp" method="post">
<input type="text" name="name"/>
<input type="submit" name="提交el2.jsp"/>
</form>
<!-- paramValues一组参数传值 -->
<form action="el2.jsp" method="post">
<input type="checkbox" name=hobby value="C语言"/>C语言
<input type="checkbox" name=hobby value="Java语言"/>Java语言
<input type="checkbox" name=hobby value="C++语言"/>C++语言
<input type="submit" name="提交el2.jsp"/>
</form>
</body>
</html>
el2.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>
<!-- 设置默认编码 -->
<%
request.setCharacterEncoding("utf-8");
%>
<h1>姓名:${param.name }</h1>
<h1>爱好1:${paramValues.hobby[0] }</h1>
<h1>爱好2:${paramValues.hobby[1] }</h1>
<h1>爱好3:${paramValues.hobby[2] }</h1>
</body>
</html>
运行结果:
5.EL表达式对象操作
首先建一个People类:
package com.java.model;
public class People {
private int id;
private String name;
private int age;
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@page import="com.java.model.People" %>
<!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>
<%
People p=new People();
p.setId(5);
p.setName("亚瑟");
p.setAge(20);
request.setAttribute("p", p);
%>
<h1>姓名:${p.name }</h1>
<h1>编号:${p.id }</h1>
<h1>年龄:${p.age }</h1>
</body>
</html>
运行结果:
6.EL表达式集合操作
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@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>
<%
List all=new LinkedList();
all.add(0,"元素1");
all.add(1,"元素2");
all.add(2,"元素3");
request.setAttribute("all", all);
%>
<h1>${all[0] }</h1>
<h1>${all[1] }</h1>
<h1>${all[2] }</h1>
</body>
</html>
运行结果: