Expression Language 表达式语⾔,替代 JSP ⻚⾯中数据访问时的复杂编码,可以⾮常便捷地取出域对象(pageContext、 request、 session、 application)中保存的数据,前提是⼀定要先setAttribute,EL 就相当于在简化 getAttribute
代替的是:request.getAttribute(“name”) 这类方法
${变量名} 变量名就是 setAttribute 对应的 key 值。
<%String name = (String) request.getAttribute("name");%> //传统方法 <%=name%> </hr> //EL表达式 ${name}pageContext -> request -> session -> application
按照上述的顺序进⾏查找,找到⽴即返回,在 application 中也⽆法找到,则返回 null
即当多个对象同时定义同一个key时,${ } 显示哪个?
<body> <% pageContext.setAttribute("name","haoqi"); request.setAttribute("name","saifei"); session.setAttribute("name","dapeng"); application.setAttribute("name","xuchao"); %> ${name} // page->haoqi </body>pageContext: ${pageScope.name}
request: ${requestScope.name}
session: ${sessionScope.name}
application: ${applicationScope.name}
<% pageContext.setAttribute("name","haoqi"); request.setAttribute("name","saifei"); session.setAttribute("name","dapeng"); application.setAttribute("name","xuchao"); %> ${pageScope.name} ${requestScope.name} ${sessionScope.name} ${applicationScope.name}User.java
package com.lut.session; public class User { private Integer id; private String name; private Double score; //重写toString,因为一般情况下 toString 得到的是: 类名 @ 哈希地址 //我们需要得到的是详细的信息。 @Override public String toString() { return "User+{" + "id=" + id + ",name=" + name + ",score=" + score + "}"; } //省略带参构造,和一堆 get,set 方法 }test01.jsp
<%@ page import="javax.jws.soap.SOAPBinding" %> <%@ page import="com.hou.web03.User" %><%-- Created by IntelliJ IDEA. User: haoqi Date: 2020/10/7 Time: 9:37 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>test01</title> </head> <body> <% User user = new User(1,"haoqi",19.0); pageContext.setAttribute("user",user); %> <!-- 级联--> ${user}<br> <!-- 获取详细信息--> ${user.id=3} ${user.name} <!--另一种语法${user["name"]}--> ${user.score} </body> </html>每一个表达式,可以用 英文单词代替使用,但是英文单词和变量之间必须用空格分隔。
&& and || or ! not == eq != ne < lt \> gt <= le \>= ge empty 变量为 null,⻓度为0的String,size为0的集合 ->空