第八章:表达式语言
一:EL简介
EL是一种简单的语言,可以方便的访问和处理应用程序数据,而无需使用JSP脚本元素或JSP表达式
二:EL语法
(1)语法:${表达式}
(2)常量:null常量${null},页面什么也不输出
(3)变量:${username},按照page,request,session,application范围的顺序依次查找名为username的属性
(4)EL中的.和[]操作符:都可以访问对象的某个属性,只是用[]必须把属性使用双引号括起来,当属性中包含特殊字符必须使用[]操作符。
三:EL隐含对象
(1)与范围有关的隐含对象:pageScope,requestScope,sessionScope,applicationScope。
(2)与请求参数有关的隐含对象:param,paramValues
(3)其它隐含对象:pageContext,header,headerValues,cookie,initParam
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="book07.Student"%>
<!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>与范围有关的隐含对象</title>
</head>
<body>
<%
pageContext.setAttribute("studentInPage", new Student("张三",21));
%>
<jsp:useBean id="studentInSession" class="book07.Student" scope="session">
<jsp:setProperty name="studentInSession" property="name" value="李四"/>
<jsp:setProperty name="studentInSession" property="age" value="22"/>
</jsp:useBean>
<p>pageContext对象中获取属性值:
${pageScope.studentInPage.name}
${pageScope.studentInPage.age}
</p>
<p>sessionScope对象中获取属性值:
${sessionScope.studentInSession.name }
${sessionScope.studentInSession.age }
</p>
</body>
</html>
public class Student {
private String name;
private int age;
public Student(){
super();
}
public Student(String name, int age){
super();
this.name = name;
this.age = age;
}
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;
}
}