Jsp学习(三)

5 篇文章 0 订阅

EL表达式

1、定义

  • 表达式语言
  • 原本是JSTL 1.0 为方便存取数据所自定义的,当时的EL 只能在JSTL 标签中使用(一般在属性中使用)
  • 从JSP 2.0 开始,EL 被正式称为标准规范之一,只要是支持Servlet 2.4 / JSP 2.0 的容器,都可以在JSP 上直接使用
  • 除了JSP 2.0 建议使用EL 外,JSF 也考虑将EL 纳入规范,JSF : Java Server Faces。
  • EL表达式是一种JSP技术,能够代替JSP中原本要用Java语言进行显示的语句,使得代码更容易编写与维护。最基本的语法是${express}。
  • 格式:    ${表达式}
  • 忽视EL表达式
    <%@ page isELIgnored = "false" %>

2、. 运算符

  • EL提供.  运算符来存取数据
  • 使用时直接在某个变量之后跟 . 来访问该变量内部的属性或方法
  • 规律:如果A 类中有getXyz() 方法,设getXyz() 方法有返回值
    若存在对象a 是A 类型,则可以使用${ a.xyz } 获取getXyz() 方法的返回值
  • 例如:User 类内部有getName() 方法,其中返回name 属性的值
    则可以使用${ user.name } 可以获取user 的name 属性的值
  • 访问自定义的对象:
    先必须将自定义对象加入作用域中
      pageContext.setAttribute("name", object);
    object的getAbc()方法可以通过EL表达式直接获取:
      ${ name.abc }

测试案例:

<%@ page language = "java" pageEncoding = "UTF-8" %>
<%@ page contentType = "text/html; charset= UTF-8"%>
<%@ page import= "java.util.*" %>
<%@ page import= "ecut.entity.*"%>
<%@ page import= "org.malajava.util.*" %>
<%-- Jsp注释通过 page指令的isELIgnored 可以告知容器是否忽悠页面上的 EL表达式--%>
<%@ page isELIgnored = "false" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Expression Language , EL</title>
<style type="text/css">
    h4 { font-weight : normal ; font-size: 20px ; height: 50px ; line-height: 50px ; margin:  0px 0px ;}
</style>
</head>
<body>

    <h2>Expression Language , EL</h2>
    
    <h3> ${ 1 + 1 } </h3>
    
    <h4> pageContext : ${ pageContext } </h4>
    
    <%--但凡是 某个对象中有 getAbc() 形式的方法,那么都可以将 getAbc() 写作abc 直接访问--%> 
    <%--比如 pageContext.getRequest() --- pageContext.request --%>
    <h4> request : ${ pageContext.request } </h4>
    
    <%--比如 pageContext.getResponse() --- pageContext.response --%> 
    <h4> response : ${ pageContext.response } </h4>
    
    <%-- pageContext.getSession()  --- pageContext.session --%>
    <h4> session : ${ pageContext.session } </h4>
    
    <%--pageContext.getServletContext() --- pageContext.servletContext--%> 
    <h4> application : ${ pageContext.servletContext } </h4>
    
    <%--pageContext.getServletConfig()  --- pageContext.servletConfig--%> 
    <h4> config : ${ pageContext.servletConfig } </h4>
    
    <%--pageContext.getOut() --- pageContext.out --%>
    <h4> out : ${ pageContext.out } </h4>
    
    <%--pageContext.getPage()  --- pageContext.page--%> 
    <h4> page : ${ pageContext.page } </h4>
    
    <%--pageContext.getException()  --- pageContext.exception --%>
    <h4> exception : ${ pageContext.exception } </h4>
    
    <hr>
    
    <%
        Student s = new Student();
        s.setId(1001);
        s.setName("华安" );
        s.setGender( '男');
        Date birthdate = DateHelper.getDate( 1996 , 10 , 11 );
        s.setBirthdate( birthdate );
        
        pageContext.setAttribute( "student" ,s); //==> Expression Language Context : pageScope
        //request.setAttribute( "student",s );==> Expression Language Context : requestScope
        //session.setAttribute( "student",s );==> Expression Language Context : sessionScope
        //application.setAttribute( "student",s );==> Expression Language Context : applicationScope
    %>
    
    <h4>
        name : <%= s.getName()%>
    </h4>
    
    <h4>
        name : ${ student.name }
    </h4>
    
    <hr>
    
    <a href="<%= request.getContextPath()%>/builtin.jsp?username=zhangsanfeng"> Expression Language 内置对象</a>
    
    <a href="${ pageContext.request.contextPath }/builtin.jsp?username=zhangsanfeng"> Expression Language 内置对象</a>
    
    
</body>
</html>
package ecut.entity;

import java.io.Serializable;
import java.util.Date;

public class Student implements Serializable {

    private static final long serialVersionUID = 3097161330831346815L;

    private Integer id;
    private String studentName;
    private char gender;
    private Date birthdate;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return studentName;
    }

    public void setName(String name) {
        this.studentName = name;
    }

    public char getGender() {
        return gender;
    }

    public void setGender(char gender) {
        this.gender = gender;
    }

    public Date getBirthdate() {
        return birthdate;
    }

    public void setBirthdate(Date birthdate) {
        this.birthdate = birthdate;
    }

}

规律一:但凡时某个对象中有getAbc()形式的方法,那么都可以写作:对象.abc直接访问

原理:EL表达式在获取某个对象的属性值时,先将某个属性值首字母变成大写,然后加上get前缀,拼接成getter方法,通过反射将该对象构建出来,然后再对该对象执行getter方法,这与私有属性并没有关系,所以要注意,JavaBean的属性名要小写,且要有getter方法,不然会报错。

3、[] 运算符

  • 可以使用类似于. 运算符的作用,形式为${ user[ "name" ] }
  • 也类似于访问数组元素的风格使用,形式为${ list[ 1 ] }

4、保留字

  • 类似于Java 中的保留字,程序员不能够在EL 中再度单独使用这些保留字
  • 保留字有:and, eq, gt, true ,or, ne ,le ,false,no ,lt ,ge, null,instanceof ,empty, div, mod

5、四大域对象

  • applicationScope 、sessionScope 、requestScope 、pageScope
  • 变量的查找顺序为p --> r --> s --> a
  • 作用范围
  1. page域:    页面范围,只能在当前jsp页面使用,通过pageContext.setAttribute()来设置                        (当前页面)  
  2. request域: 请求范围,只能在同一个请求中使用,通过pageContext.setAttribute()来设置                     (转发)  
  3. session域:会话范围, 只能在同一个会话(session对象)中使用,通过session.setAttribute()来设置       (私有的)  
  4. application域: web应用范围,只能在同一个web应用中使用,通过application.setAttribute()来设置   (全局的) 

6、EL常用的内置对象

 测试案例:

<%@ page language = "java" pageEncoding = "UTF-8" %>
<%@ page contentType = "text/html; charset= UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>EL表达式的常用内置对象</title>
</head>
<body>

    <h4>
        JSP内置对象:<%= pageContext%>
    </h4>
    
    <hr>
    
    <h4>
        pageContext : ${ pageContext }
    </h4>
    <%
    pageContext.setAttribute( "name" ,"韩小昭"); //==> Expression Language Context : pageScope
    request.setAttribute( "name" ,"张无忌"); //==> Expression Language Context :requestScope
    session.setAttribute( "name" ,"张翠山"); //==> Expression Language Context :sessionScope
    application.setAttribute( "name" ,"张三丰"); //==> Expression Language Context :applicationScope
    %>
    <h4>
        JSP表达式: <%=pageContext.getAttribute( "name" ) %>
    </h4>
    
    <h4> EL表达式 ( P - R - S - A ): ${ name }</h4>
    
    <h4>
        EL表达式: ${ pageScope.name }
    </h4>
    
    <h4>
        EL表达式: ${ requestScope.name }
    </h4>
    
    <h4>
        EL表达式: ${ sessionScope.name }
    </h4>
    
    <h4>
        EL表达式: ${ applicationScope.name }
    </h4>
    
    <hr>
    
    <h4>
        param : ${ param }
    </h4>
    
    <h4>
        username : ${ param.username }
    </h4>
    
    <h4>
        password : ${ param.password }
    </h4>
    
    <hr>
    
    
    <h2>Scope</h2>
    
    <h4>
        pageScope : ${ pageScope }
    </h4>
    
    <h4>
        requestScope : ${ requestScope }
    </h4>
    
    <h4>
        sessionScope : ${ sessionScope }
    </h4>
    
    <h4>
        applicationScope : ${ applicationScope }
    </h4>

</body>
</html>

规律二:但凡是由对象.setAttribute()用来设置的属性,都可以通过相应作用域.属性名来获取对应的值(默认按照从小到大的范围),如果是直接通过属性名来获取,则获取最小范围内的值。例如

pageContext.setAttribute( "name" ,"韩小昭");
EL表达式: ${ pageScope.name }

转载请于明显处标明出处

https://www.cnblogs.com/AmyZheng/p/9132696.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值