1. 程式人生 > >Java-Web學習筆記(第八章)

Java-Web學習筆記(第八章)

lan request 雙引號 text ava user OS 程序 Language

第八章:表達式語言

一: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;
    }

}

Java-Web學習筆記(第八章)